You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by sijusuresh <si...@gmail.com> on 2017/08/18 05:07:00 UTC

Spring Bean Injection To IgniteRunnable Implementation

I'm trying to inject a Spring Bean in IgniteRunnable which returns null.

1) Starting ignite using IgniteSpring  passing Spring Application Context.
2) In IgniteService using SchedulerFuture creating a task to run every 5
minutes
3) Task class which implements IgniteRunnable refer a Spring bean from the
context. Reference coming as NULL.Tried using @SpringResource alsoreturning
NULL in this case too.

public class FetchMetadataTask implements IgniteRunnable{

	@SpringApplicationContextResource 
    	private ApplicationContext appCtx; 

    	@Override 
    	public void run() {
    	MetaDataService metadataService =
appCtx.getBean(MetaDataService.class);
    	log.debug("FetchMetadataTask 
$MetaDataService........................"+metadataService);
    } 
}



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Spring-Bean-Injection-To-IgniteRunnable-Implementation-tp16275.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: Spring Bean Injection To IgniteRunnable Implementation

Posted by sijusuresh <si...@gmail.com>.
I was trying to get an External Spring Context. IgniteSpring.start() helped.
Thank You



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Spring-Bean-Injection-To-IgniteRunnable-Implementation-tp16275p16363.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: Spring Bean Injection To IgniteRunnable Implementation

Posted by vkulichenko <va...@gmail.com>.
First of all, I don't see MetaDataService in the XML. Why do you expect it to
be available?

Second of all, the way you start it, Ignite will not be aware of application
context at all. You need to either provide path to XML directly to
Ignition.start() method, or use IgniteSpring factory class which allows to
provide external context explicitly.

BTW, IgnitionEx is internal class, it is not supposed to be used in your
code.

-Val



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Spring-Bean-Injection-To-IgniteRunnable-Implementation-tp16275p16307.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: Spring Bean Injection To IgniteRunnable Implementation

Posted by sijusuresh <si...@gmail.com>.

This is the configuration file

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean id="igniteConfiguration"
class="org.apache.ignite.configuration.IgniteConfiguration">

    
    <property name="gridName" value="scheduler-cache-${environment.name}" />
    <property name="cacheConfiguration">
      <bean class="org.apache.ignite.configuration.CacheConfiguration">

        
        <property name="name" value="scheduler-cache-${environment.name}" />

        
        <property name="evictionPolicy">
          <bean
class="org.apache.ignite.cache.eviction.lru.LruEvictionPolicy">
            <property name="maxSize" value="10000" />
          </bean>
        </property>

        
        <property name="cacheMode" value="PARTITIONED" />
        <property name="backups" value="1" />

      </bean>
    </property>
  </bean>

</beans>

How i start the server is ..........

Resource igniteConfigResource = new
ClassPathResource("/spring/scheduler.ignite.config.xml");
String igniteConfig =
IOUtils.toString(igniteConfigResource.getInputStream());
igniteConfig = igniteConfig.replaceAll("\\$\\{environment.name\\}",
environmentName);
IgniteConfiguration igniteConfiguration = IgnitionEx.loadConfigurations(new 
ByteArrayInputStream(igniteConfig.getBytes())).get1().iterator().next();
Ignite ignite = Ignition.start(igniteConfiguration);
IgniteServices services = ignite.services(ignite.cluster().forServers());
services.deployClusterSingleton("myClusterSingletonService", new
IgniteSchedulerService());

My Ignite Service class execute method is 

@Override 
public void execute(ServiceContext ctx) throws Exception {
    	SchedulerFuture<?> fut = ignite.scheduler().scheduleLocal(new
FetchMetadataTask(appContext), "*/5 * * * *");
}



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Spring-Bean-Injection-To-IgniteRunnable-Implementation-tp16275p16300.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: Spring Bean Injection To IgniteRunnable Implementation

Posted by vkulichenko <va...@gmail.com>.
How do you start the server node where this runnable is executed? Does the
configuration contains the required bean?

-Val



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Spring-Bean-Injection-To-IgniteRunnable-Implementation-tp16275p16299.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.