You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@uima.apache.org by Spico Florin <sp...@gmail.com> on 2012/11/20 14:22:09 UTC
Dependency injection support in UIMA
Hello!
I have a connection to data source (triplestore) that is used by
multiple annotators. With the current implementation of UIMA I have to
provide this connection in for all these annotators descriptors, making the
deployment very tedious when switching from one environment to another (for
example test env to production env).
I have read the posts from
http://comments.gmane.org/gmane.comp.apache.uima.general/3340 but still is
not clear for me how to handle the DI. Basically I would like to inject
this connection to all of annotators that need it from a single
configuration file, thus eliminating the mentioned inconvenience.
Can you please provide what is the best approach for handling this kind
of problems?
I look forward for your answers.
Regards,
Florin
P.S. I was thinking to use Spring and ApplicationContext and its
configuration file. Is this a suitable solution?
Re: Dependency injection support in UIMA
Posted by Spico Florin <sp...@gmail.com>.
Hello!
I really appreciate your support Richard. I'll go for this
implementation. I hope that someone from UIMA AS will get into this topic
and validate the solution.
Regards,
Florin
On Thu, Nov 22, 2012 at 3:11 PM, Richard Eckart de Castilho <
eckart@ukp.informatik.tu-darmstadt.de> wrote:
> Hi,
>
> I'm afraid I'm not very familiar with UIMA AS. The context resource and
> consumer look reasonable to me.
>
> -- Richard
>
> Am 22.11.2012 um 14:06 schrieb Spico Florin <sp...@gmail.com>
> :
>
> > Hello!
> > I'm deploying my Consumer in AS environment. My consumer is a remote
> > component with scaleout factor of 3. The consumer tries to get an
> instance
> > to a triplestore template that has reference to a triplestore pooled
> > connection. After profiling the deployed consumer with the given set up,
> I
> > found that there is *ONE* instance of SharedResourceObject and also 3
> > instances of consumer. Can you please have a look over the following code
> > snippets and check if it is UIMA AS compliant?
> > package com;
> > public class SpringResource implements SharedResourceObject {
> > private AbstractApplicationContext context;
> > private AtomicInteger counter = new AtomicInteger();
> >
> > public void load(DataResource aData) throws
> > ResourceInitializationException {
> > context = new
> ClassPathXmlApplicationContext(aData.getUri().toString());
> > }
> >
> > public AbstractApplicationContext getContext() {
> > return context;
> > }
> > public void *registerComponent()* {
> > * counter.incrementAndGet();
> > *}
> > public void *unregisterComponent()* {
> > if (counter.get() > 0) {
> > *counter.decrementAndGet();*
> > }
> > if (counter.get() == 0) {
> > *context.close();*
> > }
> > }
> > }//consumer
> >
> > public class SpringCasConsumer extends CasConsumer_ImplBase {
> > public void initialize() throws ResourceInitializationException {
> > * * try {
> > * SpringResource shared = (SpringResource)
> > getUimaContext().getResourceObject("Resource");
> > shared.getContext().getBean("myBean");
> > shared.registerComponent();
> > * } catch (ResourceAccessException e) {
> > }
> > }
> > public void destroy() {
> > * shared.unregisterComponent();
> > super.destroy();
> > *}
> > }
> > //deployment descriptor for UIMA AS
> >
> > <analysisEngineDeploymentDescription xmlns="
> > http://uima.apache.org/resourceSpecifier">
> >
> > <name>Consumer</name>
> > <deployment protocol="jms" provider="activemq">
> > *<casPool numberOfCASes="3"/>
> > *<service>
> > <inputQueue endpoint="ConsumerQueue"
> brokerURL="${defaultBrokerURL}"/>
> > <topDescriptor>
> > <import location="../../desc/cas_consumer/SpringConsumer.xml"/>
> > </topDescriptor>
> > <analysisEngine>
> > *<scaleout numberOfInstances="3"/>*
> > </analysisEngine>
> > </service>
> > </deployment>
> > </analysisEngineDeploymentDescription>
> >
> > Configururation for resource manager:
> >
> > <resourceManagerConfiguration>
> > <externalResources>
> > <externalResource>
> > <name>*springResource*</name>
> > <description/>
> > <fileResourceSpecifier>
> > <fileUrl>file:spring_context.xml</fileUrl>
> > </fileResourceSpecifier>
> > <implementationName>*com.SpringResource*</implementationName>
> > </externalResource>
> > </externalResources>
> > <externalResourceBindings>
> > <externalResourceBinding>
> > <key*>Resource*</key>
> > <resourceName>*springResource*</resourceName>
> > </externalResourceBinding>
> > </externalResourceBindings>
> > </resourceManagerConfiguration>
> >
> > Thank you.
> > Regards,
> > Florin
>
>
> --
> -------------------------------------------------------------------
> Richard Eckart de Castilho
> Technical Lead
> Ubiquitous Knowledge Processing Lab (UKP-TUD)
> FB 20 Computer Science Department
> Technische Universität Darmstadt
> Hochschulstr. 10, D-64289 Darmstadt, Germany
> phone [+49] (0)6151 16-7477, fax -5455, room S2/02/B117
> eckart@ukp.informatik.tu-darmstadt.de
> www.ukp.tu-darmstadt.de
> Web Research at TU Darmstadt (WeRC) www.werc.tu-darmstadt.de
> -------------------------------------------------------------------
>
>
>
>
>
>
>
Re: Dependency injection support in UIMA
Posted by Richard Eckart de Castilho <ec...@ukp.informatik.tu-darmstadt.de>.
Hi,
I'm afraid I'm not very familiar with UIMA AS. The context resource and consumer look reasonable to me.
-- Richard
Am 22.11.2012 um 14:06 schrieb Spico Florin <sp...@gmail.com>
:
> Hello!
> I'm deploying my Consumer in AS environment. My consumer is a remote
> component with scaleout factor of 3. The consumer tries to get an instance
> to a triplestore template that has reference to a triplestore pooled
> connection. After profiling the deployed consumer with the given set up, I
> found that there is *ONE* instance of SharedResourceObject and also 3
> instances of consumer. Can you please have a look over the following code
> snippets and check if it is UIMA AS compliant?
> package com;
> public class SpringResource implements SharedResourceObject {
> private AbstractApplicationContext context;
> private AtomicInteger counter = new AtomicInteger();
>
> public void load(DataResource aData) throws
> ResourceInitializationException {
> context = new ClassPathXmlApplicationContext(aData.getUri().toString());
> }
>
> public AbstractApplicationContext getContext() {
> return context;
> }
> public void *registerComponent()* {
> * counter.incrementAndGet();
> *}
> public void *unregisterComponent()* {
> if (counter.get() > 0) {
> *counter.decrementAndGet();*
> }
> if (counter.get() == 0) {
> *context.close();*
> }
> }
> }//consumer
>
> public class SpringCasConsumer extends CasConsumer_ImplBase {
> public void initialize() throws ResourceInitializationException {
> * * try {
> * SpringResource shared = (SpringResource)
> getUimaContext().getResourceObject("Resource");
> shared.getContext().getBean("myBean");
> shared.registerComponent();
> * } catch (ResourceAccessException e) {
> }
> }
> public void destroy() {
> * shared.unregisterComponent();
> super.destroy();
> *}
> }
> //deployment descriptor for UIMA AS
>
> <analysisEngineDeploymentDescription xmlns="
> http://uima.apache.org/resourceSpecifier">
>
> <name>Consumer</name>
> <deployment protocol="jms" provider="activemq">
> *<casPool numberOfCASes="3"/>
> *<service>
> <inputQueue endpoint="ConsumerQueue" brokerURL="${defaultBrokerURL}"/>
> <topDescriptor>
> <import location="../../desc/cas_consumer/SpringConsumer.xml"/>
> </topDescriptor>
> <analysisEngine>
> *<scaleout numberOfInstances="3"/>*
> </analysisEngine>
> </service>
> </deployment>
> </analysisEngineDeploymentDescription>
>
> Configururation for resource manager:
>
> <resourceManagerConfiguration>
> <externalResources>
> <externalResource>
> <name>*springResource*</name>
> <description/>
> <fileResourceSpecifier>
> <fileUrl>file:spring_context.xml</fileUrl>
> </fileResourceSpecifier>
> <implementationName>*com.SpringResource*</implementationName>
> </externalResource>
> </externalResources>
> <externalResourceBindings>
> <externalResourceBinding>
> <key*>Resource*</key>
> <resourceName>*springResource*</resourceName>
> </externalResourceBinding>
> </externalResourceBindings>
> </resourceManagerConfiguration>
>
> Thank you.
> Regards,
> Florin
--
-------------------------------------------------------------------
Richard Eckart de Castilho
Technical Lead
Ubiquitous Knowledge Processing Lab (UKP-TUD)
FB 20 Computer Science Department
Technische Universität Darmstadt
Hochschulstr. 10, D-64289 Darmstadt, Germany
phone [+49] (0)6151 16-7477, fax -5455, room S2/02/B117
eckart@ukp.informatik.tu-darmstadt.de
www.ukp.tu-darmstadt.de
Web Research at TU Darmstadt (WeRC) www.werc.tu-darmstadt.de
-------------------------------------------------------------------
Re: Dependency injection support in UIMA
Posted by Spico Florin <sp...@gmail.com>.
Hello!
I'm deploying my Consumer in AS environment. My consumer is a remote
component with scaleout factor of 3. The consumer tries to get an instance
to a triplestore template that has reference to a triplestore pooled
connection. After profiling the deployed consumer with the given set up, I
found that there is *ONE* instance of SharedResourceObject and also 3
instances of consumer. Can you please have a look over the following code
snippets and check if it is UIMA AS compliant?
package com;
public class SpringResource implements SharedResourceObject {
private AbstractApplicationContext context;
private AtomicInteger counter = new AtomicInteger();
public void load(DataResource aData) throws
ResourceInitializationException {
context = new ClassPathXmlApplicationContext(aData.getUri().toString());
}
public AbstractApplicationContext getContext() {
return context;
}
public void *registerComponent()* {
* counter.incrementAndGet();
*}
public void *unregisterComponent()* {
if (counter.get() > 0) {
*counter.decrementAndGet();*
}
if (counter.get() == 0) {
*context.close();*
}
}
}//consumer
public class SpringCasConsumer extends CasConsumer_ImplBase {
public void initialize() throws ResourceInitializationException {
* * try {
* SpringResource shared = (SpringResource)
getUimaContext().getResourceObject("Resource");
shared.getContext().getBean("myBean");
shared.registerComponent();
* } catch (ResourceAccessException e) {
}
}
public void destroy() {
* shared.unregisterComponent();
super.destroy();
*}
}
//deployment descriptor for UIMA AS
<analysisEngineDeploymentDescription xmlns="
http://uima.apache.org/resourceSpecifier">
<name>Consumer</name>
<deployment protocol="jms" provider="activemq">
*<casPool numberOfCASes="3"/>
*<service>
<inputQueue endpoint="ConsumerQueue" brokerURL="${defaultBrokerURL}"/>
<topDescriptor>
<import location="../../desc/cas_consumer/SpringConsumer.xml"/>
</topDescriptor>
<analysisEngine>
*<scaleout numberOfInstances="3"/>*
</analysisEngine>
</service>
</deployment>
</analysisEngineDeploymentDescription>
Configururation for resource manager:
<resourceManagerConfiguration>
<externalResources>
<externalResource>
<name>*springResource*</name>
<description/>
<fileResourceSpecifier>
<fileUrl>file:spring_context.xml</fileUrl>
</fileResourceSpecifier>
<implementationName>*com.SpringResource*</implementationName>
</externalResource>
</externalResources>
<externalResourceBindings>
<externalResourceBinding>
<key*>Resource*</key>
<resourceName>*springResource*</resourceName>
</externalResourceBinding>
</externalResourceBindings>
</resourceManagerConfiguration>
Thank you.
Regards,
Florin
On Wed, Nov 21, 2012 at 8:25 PM, Richard Eckart de Castilho <
eckart@ukp.informatik.tu-darmstadt.de> wrote:
> Hi,
> > Thank you for your anwers. Regarding the SharedResourceObject approach
> > in the JavaDoc I didn't find any reference about how many instances will
> be
> > created when you have many Annotators using the same resource. I would
> say
> > that that will be one SharedResouce per instance Annotator.
>
> The ResourceManager creates one instance per resource per UIMA context.
>
> > 1.So suppose that you have AnnotatorX and AnnotatorY using the
> > SharedResourceObject collocated (same JVM) how many instances of
> > SharedResourceObject do we have?
>
> Depends how you deploy your pipelines. Assuming that run a single CPE
> (or something the likes) per JVM, there will be a single instance. Each
> CPE run creates an UIMA context internally. I don't know how UIMA-AS
> handles
> that. If you run your pipelines with uimaFIT's SimplePipeline, one instance
> per call run runPipeline() is created.
>
> > 2.Also, if the AnnotatorX is a remote EA that is using the
> > SharedResourceObject with a scale out factor of 3, how many instances of
> > SharedResourceObject will be created?
>
> As the name says, the instance is shared - so one instance.
>
> > What I'm afraid, that given the ApplicationContext on some point we have
> > to destroy it. As far as I know this can be achieve in the destroy method
> > of the annotator. But if we have a single SharedResourceObject shared
> among
> > annotators probably will be a mess to destroy the context from each of
> the
> > instances.
>
> It seems that UIMA does currently not support a full lifecycle management
> for external resources. It can create the, but they are not notified in
> any way
> when the UIMA context/ResourceManager goes out of business.
>
> You could implement a check-out/return mechanism: increment a counter
> variable
> in the resource every time an analysis engine calls a certain method on the
> resource. In the destroy() method of the analysis engine, call another
> method
> to decrement the counter. If the counter reaches 0 at the end of the
> decrement method, call your resource's destroy() logic.
>
> > I would like to ask you if this code is proper solution for our case
> >
> > MyCasConsumer {
> >
> > private ApplicationContext springContext;
> >
> > void initialize() {
> > springContext = new
> ClassPathXmlApplicationContext("spring-context.xml");
> > }
> >
> > void destroy() {
> > springContext.close();
> > }
> > }
>
> I wouldn't call a shared resource "CasConsumer". Also, CasConsumer's
> normally
> don't scale because they have the MULTIPLE_DEPLOYMENT_ALLOWED_DEFAULT flag
> set
> to "false".
>
> -- Richard
>
> --
> -------------------------------------------------------------------
> Richard Eckart de Castilho
> Technical Lead
> Ubiquitous Knowledge Processing Lab (UKP-TUD)
> FB 20 Computer Science Department
> Technische Universität Darmstadt
> Hochschulstr. 10, D-64289 Darmstadt, Germany
> phone [+49] (0)6151 16-7477, fax -5455, room S2/02/B117
> eckart@ukp.informatik.tu-darmstadt.de
> www.ukp.tu-darmstadt.de
> Web Research at TU Darmstadt (WeRC) www.werc.tu-darmstadt.de
> -------------------------------------------------------------------
>
>
>
>
>
>
>
Re: Dependency injection support in UIMA
Posted by Richard Eckart de Castilho <ec...@ukp.informatik.tu-darmstadt.de>.
Hi,
> Thank you for your anwers. Regarding the SharedResourceObject approach
> in the JavaDoc I didn't find any reference about how many instances will be
> created when you have many Annotators using the same resource. I would say
> that that will be one SharedResouce per instance Annotator.
The ResourceManager creates one instance per resource per UIMA context.
> 1.So suppose that you have AnnotatorX and AnnotatorY using the
> SharedResourceObject collocated (same JVM) how many instances of
> SharedResourceObject do we have?
Depends how you deploy your pipelines. Assuming that run a single CPE
(or something the likes) per JVM, there will be a single instance. Each
CPE run creates an UIMA context internally. I don't know how UIMA-AS handles
that. If you run your pipelines with uimaFIT's SimplePipeline, one instance
per call run runPipeline() is created.
> 2.Also, if the AnnotatorX is a remote EA that is using the
> SharedResourceObject with a scale out factor of 3, how many instances of
> SharedResourceObject will be created?
As the name says, the instance is shared - so one instance.
> What I'm afraid, that given the ApplicationContext on some point we have
> to destroy it. As far as I know this can be achieve in the destroy method
> of the annotator. But if we have a single SharedResourceObject shared among
> annotators probably will be a mess to destroy the context from each of the
> instances.
It seems that UIMA does currently not support a full lifecycle management
for external resources. It can create the, but they are not notified in any way
when the UIMA context/ResourceManager goes out of business.
You could implement a check-out/return mechanism: increment a counter variable
in the resource every time an analysis engine calls a certain method on the
resource. In the destroy() method of the analysis engine, call another method
to decrement the counter. If the counter reaches 0 at the end of the
decrement method, call your resource's destroy() logic.
> I would like to ask you if this code is proper solution for our case
>
> MyCasConsumer {
>
> private ApplicationContext springContext;
>
> void initialize() {
> springContext = new ClassPathXmlApplicationContext("spring-context.xml");
> }
>
> void destroy() {
> springContext.close();
> }
> }
I wouldn't call a shared resource "CasConsumer". Also, CasConsumer's normally
don't scale because they have the MULTIPLE_DEPLOYMENT_ALLOWED_DEFAULT flag set
to "false".
-- Richard
--
-------------------------------------------------------------------
Richard Eckart de Castilho
Technical Lead
Ubiquitous Knowledge Processing Lab (UKP-TUD)
FB 20 Computer Science Department
Technische Universität Darmstadt
Hochschulstr. 10, D-64289 Darmstadt, Germany
phone [+49] (0)6151 16-7477, fax -5455, room S2/02/B117
eckart@ukp.informatik.tu-darmstadt.de
www.ukp.tu-darmstadt.de
Web Research at TU Darmstadt (WeRC) www.werc.tu-darmstadt.de
-------------------------------------------------------------------
Re: Dependency injection support in UIMA
Posted by Spico Florin <sp...@gmail.com>.
Hello!
Thank you for your anwers. Regarding the SharedResourceObject approach
in the JavaDoc I didn't find any reference about how many instances will be
created when you have many Annotators using the same resource. I would say
that that will be one SharedResouce per instance Annotator.
1.So suppose that you have AnnotatorX and AnnotatorY using the
SharedResourceObject collocated (same JVM) how many instances of
SharedResourceObject do we have?
2.Also, if the AnnotatorX is a remote EA that is using the
SharedResourceObject with a scale out factor of 3, how many instances of
SharedResourceObject will be created?
What I'm afraid, that given the ApplicationContext on some point we have
to destroy it. As far as I know this can be achieve in the destroy method
of the annotator. But if we have a single SharedResourceObject shared among
annotators probably will be a mess to destroy the context from each of the
instances.
I would like to ask you if this code is proper solution for our case
MyCasConsumer {
private ApplicationContext springContext;
void initialize() {
springContext = new ClassPathXmlApplicationContext("spring-context.xml");
}
void destroy() {
springContext.close();
}
}
I look forward for your answers.
Regards,
Florin
On Tue, Nov 20, 2012 at 6:27 PM, Richard Eckart de Castilho <
eckart@ukp.informatik.tu-darmstadt.de> wrote:
> Hello Florin,
>
> this is not an issue with Spring but with the injection pre-existing
> instances in general. With "pre-existing" I mean instances that are not
> managed by the UIMA framework. If you deploy a UIMA component somewhere,
> the UIMA framework usually knows how to create all involved instances. Now
> let's say you want to your UIMA component in a Spring application and in
> that application you have a database connection pool. Now you may want to
> create a CasConsumer that uses this existing DB connection pool to write
> stuff to a database. Obviously the UIMA framework shouldn't create the
> pool, you only need some way to may it accessible within the CasConsumer.
> That's where you can use the methods I have outlined.
>
> Now regarding scale-out. Your CasConsumer would of course only work
> somewhere, where such a DB connection pool is already present, because UIMA
> does not know how to create such a pool for you. So you have to make sure
> that in each environment that you use for your scale-out, the pool is
> somehow set up.
>
> If you do not want to use pre-existing instances, but only want to use a
> Spring application context XML file for configuration, you could just
> implement a normal UIMA external resource, have it load the XML file and
> return the ApplicationContext. There wouldn't be any particular dependency
> injection involved in that on the UIMA side though. Something like this I
> suppose (untested):
>
> class SpringResource implements SharedResourceObject {
> private ApplicationContext context;
>
> public void load(DataResource aData) throws
> ResourceInitializationException {
> context = new
> ClassPathXmlApplicationContext(aData.getUri().toString());
> }
>
> public String getContext() {
> return context;
> }
> }
>
> You can point the URI to a file or to a classpath location which contains
> different Spring configurations on your test and on your production
> environment.
>
> -- Richard
>
> Am 20.11.2012 um 16:58 schrieb Spico Florin <sp...@gmail.com>
> :
>
> > Hello!
> > Thank you for your response Richard. As far as I know (please correct
> me
> > if I'm wrong) the scale out is referring to create many instances of the
> > same analyzing engine that will process CASes. The Analyzing engine
> should
> > be also remote AE.
> > Can you please provide more details why adding how the objects created
> > with Spring affect the scale-out capability?
> >
> > Regards,
> > Florin
> >
> > On Tue, Nov 20, 2012 at 5:07 PM, Richard Eckart de Castilho <
> > eckart@ukp.informatik.tu-darmstadt.de> wrote:
> >
> >> Hello Florin,
> >>
> >> first of all, UIMA does not support injections of pre-existing
> instances.
> >> You are not supposed to access any object instances that are not created
> >> within the context of UIMA. If you can live with the consequences of
> doing
> >> this (i.e. no UIMA supported scale-out), you can have a look at the
> >> following possibilities that I have implemented as proof-of-concept in
> >> uimaFIT:
> >>
> >>
> >>
> >> == SimpleNamedResourceManager ==
> >>
> >> The uimaFIT SimpleNamedResourceManager takes a key-value map of names
> and
> >> instances. When you create your analysis instance with this resource
> >> manager, you can fetch the instances via the UIMA external resource
> >> mechanism. Example:
> >>
> >>
> >> // Create a custom resource manager that allows to inject any Java
> object
> >> as an external
> >> // dependency
> >> final Map<String, Object> externalContext = new HashMap<String,
> Object>();
> >> externalContext.put("pojoName2", new AtomicInteger(5));
> >>
> >> // Create resource manager
> >> SimpleNamedResourceManager resMgr = new SimpleNamedResourceManager();
> >> resMgr.setExternalContext(externalContext);
> >>
> >> // Instantiate component
> >> AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc, resMgr,
> >> null);
> >>
> >> More test code is here:
> >>
> >>
> >>
> http://code.google.com/p/uimafit/source/browse/trunk/uimaFIT/src/test/java/org/uimafit/factory/ExternalResourceFactoryTest.java
> >>
> >>
> >>
> >> == SpringContextResourceManager ==
> >>
> >> The uimaFIT SpringContextResourceManager makes all Spring beans from a
> >> given Spring application context available via the UIMA external
> resource
> >> mechanism. Example:
> >>
> >> // Acquire application context
> >> ApplicationContext ctx = getApplicationContext();
> >>
> >> // Create resource manager
> >> SpringContextResourceManager resMgr = new
> SpringContextResourceManager();
> >> resMgr.setApplicationContext(ctx);
> >>
> >> // Instantiate component
> >> AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc, resMgr,
> >> null);
> >>
> >> See also:
> >>
> >>
> >>
> http://code.google.com/p/uimafit/source/browse/trunk/uimaFIT-spring/src/test/java/org/uimafit/spring/SpringContextResourceManagerTest.java
> >>
> >>
> >>
> >> == UIMA Factory Patching ==
> >>
> >> This is an invasive mechanism that patches the UIMA framework at runtime
> >> so that every UIMA component that is created is post-processed by a
> Spring
> >> application context. This means, UIMA components can directly use any
> >> annotations supported by Spring (e.g. @PersistenceContext, …). Only one
> >> application context is supported per JVM because the UIMA framework is
> >> (unfortunately) internally statically configured. This should work, but
> I
> >> didn't really test it yet beyond the proof-of-concept unit test.
> >>
> >> Example code:
> >>
> >>
> >>
> http://code.google.com/p/uimafit/source/browse/trunk/uimaFIT-spring/src/test/java/org/uimafit/spring/UimaFactoryInjectionTest.java
> >>
> >> Cheers,
> >>
> >> -- Richard
> >>
> >> Am 20.11.2012 um 14:22 schrieb Spico Florin <sp...@gmail.com>:
> >>
> >>> Hello!
> >>> I have a connection to data source (triplestore) that is used by
> >>> multiple annotators. With the current implementation of UIMA I have to
> >>> provide this connection in for all these annotators descriptors, making
> >> the
> >>> deployment very tedious when switching from one environment to another
> >> (for
> >>> example test env to production env).
> >>> I have read the posts from
> >>> http://comments.gmane.org/gmane.comp.apache.uima.general/3340 but
> still
> >> is
> >>> not clear for me how to handle the DI. Basically I would like to inject
> >>> this connection to all of annotators that need it from a single
> >>> configuration file, thus eliminating the mentioned inconvenience.
> >>> Can you please provide what is the best approach for handling this kind
> >>> of problems?
> >>>
> >>> I look forward for your answers.
> >>>
> >>> Regards,
> >>> Florin
> >>>
> >>> P.S. I was thinking to use Spring and ApplicationContext and its
> >>> configuration file. Is this a suitable solution?
>
>
> --
> -------------------------------------------------------------------
> Richard Eckart de Castilho
> Technical Lead
> Ubiquitous Knowledge Processing Lab (UKP-TUD)
> FB 20 Computer Science Department
> Technische Universität Darmstadt
> Hochschulstr. 10, D-64289 Darmstadt, Germany
> phone [+49] (0)6151 16-7477, fax -5455, room S2/02/B117
> eckart@ukp.informatik.tu-darmstadt.de
> www.ukp.tu-darmstadt.de
> Web Research at TU Darmstadt (WeRC) www.werc.tu-darmstadt.de
> -------------------------------------------------------------------
>
>
>
>
>
>
>
Re: Dependency injection support in UIMA
Posted by Richard Eckart de Castilho <ec...@ukp.informatik.tu-darmstadt.de>.
Hello Florin,
this is not an issue with Spring but with the injection pre-existing instances in general. With "pre-existing" I mean instances that are not managed by the UIMA framework. If you deploy a UIMA component somewhere, the UIMA framework usually knows how to create all involved instances. Now let's say you want to your UIMA component in a Spring application and in that application you have a database connection pool. Now you may want to create a CasConsumer that uses this existing DB connection pool to write stuff to a database. Obviously the UIMA framework shouldn't create the pool, you only need some way to may it accessible within the CasConsumer. That's where you can use the methods I have outlined.
Now regarding scale-out. Your CasConsumer would of course only work somewhere, where such a DB connection pool is already present, because UIMA does not know how to create such a pool for you. So you have to make sure that in each environment that you use for your scale-out, the pool is somehow set up.
If you do not want to use pre-existing instances, but only want to use a Spring application context XML file for configuration, you could just implement a normal UIMA external resource, have it load the XML file and return the ApplicationContext. There wouldn't be any particular dependency injection involved in that on the UIMA side though. Something like this I suppose (untested):
class SpringResource implements SharedResourceObject {
private ApplicationContext context;
public void load(DataResource aData) throws ResourceInitializationException {
context = new ClassPathXmlApplicationContext(aData.getUri().toString());
}
public String getContext() {
return context;
}
}
You can point the URI to a file or to a classpath location which contains different Spring configurations on your test and on your production environment.
-- Richard
Am 20.11.2012 um 16:58 schrieb Spico Florin <sp...@gmail.com>
:
> Hello!
> Thank you for your response Richard. As far as I know (please correct me
> if I'm wrong) the scale out is referring to create many instances of the
> same analyzing engine that will process CASes. The Analyzing engine should
> be also remote AE.
> Can you please provide more details why adding how the objects created
> with Spring affect the scale-out capability?
>
> Regards,
> Florin
>
> On Tue, Nov 20, 2012 at 5:07 PM, Richard Eckart de Castilho <
> eckart@ukp.informatik.tu-darmstadt.de> wrote:
>
>> Hello Florin,
>>
>> first of all, UIMA does not support injections of pre-existing instances.
>> You are not supposed to access any object instances that are not created
>> within the context of UIMA. If you can live with the consequences of doing
>> this (i.e. no UIMA supported scale-out), you can have a look at the
>> following possibilities that I have implemented as proof-of-concept in
>> uimaFIT:
>>
>>
>>
>> == SimpleNamedResourceManager ==
>>
>> The uimaFIT SimpleNamedResourceManager takes a key-value map of names and
>> instances. When you create your analysis instance with this resource
>> manager, you can fetch the instances via the UIMA external resource
>> mechanism. Example:
>>
>>
>> // Create a custom resource manager that allows to inject any Java object
>> as an external
>> // dependency
>> final Map<String, Object> externalContext = new HashMap<String, Object>();
>> externalContext.put("pojoName2", new AtomicInteger(5));
>>
>> // Create resource manager
>> SimpleNamedResourceManager resMgr = new SimpleNamedResourceManager();
>> resMgr.setExternalContext(externalContext);
>>
>> // Instantiate component
>> AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc, resMgr,
>> null);
>>
>> More test code is here:
>>
>>
>> http://code.google.com/p/uimafit/source/browse/trunk/uimaFIT/src/test/java/org/uimafit/factory/ExternalResourceFactoryTest.java
>>
>>
>>
>> == SpringContextResourceManager ==
>>
>> The uimaFIT SpringContextResourceManager makes all Spring beans from a
>> given Spring application context available via the UIMA external resource
>> mechanism. Example:
>>
>> // Acquire application context
>> ApplicationContext ctx = getApplicationContext();
>>
>> // Create resource manager
>> SpringContextResourceManager resMgr = new SpringContextResourceManager();
>> resMgr.setApplicationContext(ctx);
>>
>> // Instantiate component
>> AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc, resMgr,
>> null);
>>
>> See also:
>>
>>
>> http://code.google.com/p/uimafit/source/browse/trunk/uimaFIT-spring/src/test/java/org/uimafit/spring/SpringContextResourceManagerTest.java
>>
>>
>>
>> == UIMA Factory Patching ==
>>
>> This is an invasive mechanism that patches the UIMA framework at runtime
>> so that every UIMA component that is created is post-processed by a Spring
>> application context. This means, UIMA components can directly use any
>> annotations supported by Spring (e.g. @PersistenceContext, …). Only one
>> application context is supported per JVM because the UIMA framework is
>> (unfortunately) internally statically configured. This should work, but I
>> didn't really test it yet beyond the proof-of-concept unit test.
>>
>> Example code:
>>
>>
>> http://code.google.com/p/uimafit/source/browse/trunk/uimaFIT-spring/src/test/java/org/uimafit/spring/UimaFactoryInjectionTest.java
>>
>> Cheers,
>>
>> -- Richard
>>
>> Am 20.11.2012 um 14:22 schrieb Spico Florin <sp...@gmail.com>:
>>
>>> Hello!
>>> I have a connection to data source (triplestore) that is used by
>>> multiple annotators. With the current implementation of UIMA I have to
>>> provide this connection in for all these annotators descriptors, making
>> the
>>> deployment very tedious when switching from one environment to another
>> (for
>>> example test env to production env).
>>> I have read the posts from
>>> http://comments.gmane.org/gmane.comp.apache.uima.general/3340 but still
>> is
>>> not clear for me how to handle the DI. Basically I would like to inject
>>> this connection to all of annotators that need it from a single
>>> configuration file, thus eliminating the mentioned inconvenience.
>>> Can you please provide what is the best approach for handling this kind
>>> of problems?
>>>
>>> I look forward for your answers.
>>>
>>> Regards,
>>> Florin
>>>
>>> P.S. I was thinking to use Spring and ApplicationContext and its
>>> configuration file. Is this a suitable solution?
--
-------------------------------------------------------------------
Richard Eckart de Castilho
Technical Lead
Ubiquitous Knowledge Processing Lab (UKP-TUD)
FB 20 Computer Science Department
Technische Universität Darmstadt
Hochschulstr. 10, D-64289 Darmstadt, Germany
phone [+49] (0)6151 16-7477, fax -5455, room S2/02/B117
eckart@ukp.informatik.tu-darmstadt.de
www.ukp.tu-darmstadt.de
Web Research at TU Darmstadt (WeRC) www.werc.tu-darmstadt.de
-------------------------------------------------------------------
Re: Dependency injection support in UIMA
Posted by Spico Florin <sp...@gmail.com>.
Hello!
Thank you for your response Richard. As far as I know (please correct me
if I'm wrong) the scale out is referring to create many instances of the
same analyzing engine that will process CASes. The Analyzing engine should
be also remote AE.
Can you please provide more details why adding how the objects created
with Spring affect the scale-out capability?
Regards,
Florin
On Tue, Nov 20, 2012 at 5:07 PM, Richard Eckart de Castilho <
eckart@ukp.informatik.tu-darmstadt.de> wrote:
> Hello Florin,
>
> first of all, UIMA does not support injections of pre-existing instances.
> You are not supposed to access any object instances that are not created
> within the context of UIMA. If you can live with the consequences of doing
> this (i.e. no UIMA supported scale-out), you can have a look at the
> following possibilities that I have implemented as proof-of-concept in
> uimaFIT:
>
>
>
> == SimpleNamedResourceManager ==
>
> The uimaFIT SimpleNamedResourceManager takes a key-value map of names and
> instances. When you create your analysis instance with this resource
> manager, you can fetch the instances via the UIMA external resource
> mechanism. Example:
>
>
> // Create a custom resource manager that allows to inject any Java object
> as an external
> // dependency
> final Map<String, Object> externalContext = new HashMap<String, Object>();
> externalContext.put("pojoName2", new AtomicInteger(5));
>
> // Create resource manager
> SimpleNamedResourceManager resMgr = new SimpleNamedResourceManager();
> resMgr.setExternalContext(externalContext);
>
> // Instantiate component
> AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc, resMgr,
> null);
>
> More test code is here:
>
>
> http://code.google.com/p/uimafit/source/browse/trunk/uimaFIT/src/test/java/org/uimafit/factory/ExternalResourceFactoryTest.java
>
>
>
> == SpringContextResourceManager ==
>
> The uimaFIT SpringContextResourceManager makes all Spring beans from a
> given Spring application context available via the UIMA external resource
> mechanism. Example:
>
> // Acquire application context
> ApplicationContext ctx = getApplicationContext();
>
> // Create resource manager
> SpringContextResourceManager resMgr = new SpringContextResourceManager();
> resMgr.setApplicationContext(ctx);
>
> // Instantiate component
> AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc, resMgr,
> null);
>
> See also:
>
>
> http://code.google.com/p/uimafit/source/browse/trunk/uimaFIT-spring/src/test/java/org/uimafit/spring/SpringContextResourceManagerTest.java
>
>
>
> == UIMA Factory Patching ==
>
> This is an invasive mechanism that patches the UIMA framework at runtime
> so that every UIMA component that is created is post-processed by a Spring
> application context. This means, UIMA components can directly use any
> annotations supported by Spring (e.g. @PersistenceContext, …). Only one
> application context is supported per JVM because the UIMA framework is
> (unfortunately) internally statically configured. This should work, but I
> didn't really test it yet beyond the proof-of-concept unit test.
>
> Example code:
>
>
> http://code.google.com/p/uimafit/source/browse/trunk/uimaFIT-spring/src/test/java/org/uimafit/spring/UimaFactoryInjectionTest.java
>
> Cheers,
>
> -- Richard
>
> Am 20.11.2012 um 14:22 schrieb Spico Florin <sp...@gmail.com>:
>
> > Hello!
> > I have a connection to data source (triplestore) that is used by
> > multiple annotators. With the current implementation of UIMA I have to
> > provide this connection in for all these annotators descriptors, making
> the
> > deployment very tedious when switching from one environment to another
> (for
> > example test env to production env).
> > I have read the posts from
> > http://comments.gmane.org/gmane.comp.apache.uima.general/3340 but still
> is
> > not clear for me how to handle the DI. Basically I would like to inject
> > this connection to all of annotators that need it from a single
> > configuration file, thus eliminating the mentioned inconvenience.
> > Can you please provide what is the best approach for handling this kind
> > of problems?
> >
> > I look forward for your answers.
> >
> > Regards,
> > Florin
> >
> > P.S. I was thinking to use Spring and ApplicationContext and its
> > configuration file. Is this a suitable solution?
>
>
> --
> -------------------------------------------------------------------
> Richard Eckart de Castilho
> Technical Lead
> Ubiquitous Knowledge Processing Lab (UKP-TUD)
> FB 20 Computer Science Department
> Technische Universität Darmstadt
> Hochschulstr. 10, D-64289 Darmstadt, Germany
> phone [+49] (0)6151 16-7477, fax -5455, room S2/02/B117
> eckart@ukp.informatik.tu-darmstadt.de
> www.ukp.tu-darmstadt.de
> Web Research at TU Darmstadt (WeRC) www.werc.tu-darmstadt.de
> -------------------------------------------------------------------
>
>
>
>
>
>
>
Re: Dependency injection support in UIMA
Posted by Richard Eckart de Castilho <ec...@ukp.informatik.tu-darmstadt.de>.
Hello Florin,
first of all, UIMA does not support injections of pre-existing instances. You are not supposed to access any object instances that are not created within the context of UIMA. If you can live with the consequences of doing this (i.e. no UIMA supported scale-out), you can have a look at the following possibilities that I have implemented as proof-of-concept in uimaFIT:
== SimpleNamedResourceManager ==
The uimaFIT SimpleNamedResourceManager takes a key-value map of names and instances. When you create your analysis instance with this resource manager, you can fetch the instances via the UIMA external resource mechanism. Example:
// Create a custom resource manager that allows to inject any Java object as an external
// dependency
final Map<String, Object> externalContext = new HashMap<String, Object>();
externalContext.put("pojoName2", new AtomicInteger(5));
// Create resource manager
SimpleNamedResourceManager resMgr = new SimpleNamedResourceManager();
resMgr.setExternalContext(externalContext);
// Instantiate component
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc, resMgr, null);
More test code is here:
http://code.google.com/p/uimafit/source/browse/trunk/uimaFIT/src/test/java/org/uimafit/factory/ExternalResourceFactoryTest.java
== SpringContextResourceManager ==
The uimaFIT SpringContextResourceManager makes all Spring beans from a given Spring application context available via the UIMA external resource mechanism. Example:
// Acquire application context
ApplicationContext ctx = getApplicationContext();
// Create resource manager
SpringContextResourceManager resMgr = new SpringContextResourceManager();
resMgr.setApplicationContext(ctx);
// Instantiate component
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc, resMgr, null);
See also:
http://code.google.com/p/uimafit/source/browse/trunk/uimaFIT-spring/src/test/java/org/uimafit/spring/SpringContextResourceManagerTest.java
== UIMA Factory Patching ==
This is an invasive mechanism that patches the UIMA framework at runtime so that every UIMA component that is created is post-processed by a Spring application context. This means, UIMA components can directly use any annotations supported by Spring (e.g. @PersistenceContext, …). Only one application context is supported per JVM because the UIMA framework is (unfortunately) internally statically configured. This should work, but I didn't really test it yet beyond the proof-of-concept unit test.
Example code:
http://code.google.com/p/uimafit/source/browse/trunk/uimaFIT-spring/src/test/java/org/uimafit/spring/UimaFactoryInjectionTest.java
Cheers,
-- Richard
Am 20.11.2012 um 14:22 schrieb Spico Florin <sp...@gmail.com>:
> Hello!
> I have a connection to data source (triplestore) that is used by
> multiple annotators. With the current implementation of UIMA I have to
> provide this connection in for all these annotators descriptors, making the
> deployment very tedious when switching from one environment to another (for
> example test env to production env).
> I have read the posts from
> http://comments.gmane.org/gmane.comp.apache.uima.general/3340 but still is
> not clear for me how to handle the DI. Basically I would like to inject
> this connection to all of annotators that need it from a single
> configuration file, thus eliminating the mentioned inconvenience.
> Can you please provide what is the best approach for handling this kind
> of problems?
>
> I look forward for your answers.
>
> Regards,
> Florin
>
> P.S. I was thinking to use Spring and ApplicationContext and its
> configuration file. Is this a suitable solution?
--
-------------------------------------------------------------------
Richard Eckart de Castilho
Technical Lead
Ubiquitous Knowledge Processing Lab (UKP-TUD)
FB 20 Computer Science Department
Technische Universität Darmstadt
Hochschulstr. 10, D-64289 Darmstadt, Germany
phone [+49] (0)6151 16-7477, fax -5455, room S2/02/B117
eckart@ukp.informatik.tu-darmstadt.de
www.ukp.tu-darmstadt.de
Web Research at TU Darmstadt (WeRC) www.werc.tu-darmstadt.de
-------------------------------------------------------------------
Re: Dependency injection support in UIMA
Posted by Marshall Schor <ms...@schor.com>.
Hi,
The next release of UIMA will have a facility that augments the existing methods
of setting parameters with an additional one based on (a slight extension of)
Java properties files. It's purpose is to collect into one spot all the
parameter settings needed for a complex set of annotators. The original working
discussion for this is on our wiki; I think what got implemented is close to
that (but not exactly), see
https://cwiki.apache.org/UIMA/configuring-uima-pipelines-externally-for-a-particular-run.html
-Marshall
On 11/20/2012 8:22 AM, Spico Florin wrote:
> Hello!
> I have a connection to data source (triplestore) that is used by
> multiple annotators. With the current implementation of UIMA I have to
> provide this connection in for all these annotators descriptors, making the
> deployment very tedious when switching from one environment to another (for
> example test env to production env).
> I have read the posts from
> http://comments.gmane.org/gmane.comp.apache.uima.general/3340 but still is
> not clear for me how to handle the DI. Basically I would like to inject
> this connection to all of annotators that need it from a single
> configuration file, thus eliminating the mentioned inconvenience.
> Can you please provide what is the best approach for handling this kind
> of problems?
>
> I look forward for your answers.
>
> Regards,
> Florin
>
> P.S. I was thinking to use Spring and ApplicationContext and its
> configuration file. Is this a suitable solution?
>