Using @SpringResource & @SpringApplicationContextResource in IgniteCallable

classic Classic list List threaded Threaded
4 messages Options
ashishb888 ashishb888
Reply | Threaded
Open this post in threaded view
|

Using @SpringResource & @SpringApplicationContextResource in IgniteCallable

static class ICCall implements IgniteCallable<Long> {
                private static final long serialVersionUID = 4278959731940740185L;

                @IgniteInstanceResource
                private Ignite ignite;
                @SpringResource(resourceName = "testService")
                private TestService ts;
                @SpringApplicationContextResource
                private ApplicationContext ac;

                private int affKey;
                private String cacheName;

                ICCall(int affKey, String cacheName) {
                        this.affKey = affKey;
                        this.cacheName = cacheName;
                }

                @Override
                public Long call() {
                        log.debug("affKey: " + affKey + ", cacheName: " + cacheName);
                        log.debug("beans: " + Arrays.toString(ac.getBeanDefinitionNames()));

                        return ts.call(affKey, cacheName);
                }
        }

I am not able to get ApplicationContext by using
@SpringApplicationContextResource. So getting null pointer exception. Did I
miss something?
How one can use @SpringResource & @SpringApplicationContextResource in
IgniteCallable?

BR,
Ashish





--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
ashishb888 ashishb888
Reply | Threaded
Open this post in threaded view
|

Re: Using @SpringResource & @SpringApplicationContextResource in IgniteCallable

Ilya Kazakov Ilya Kazakov
Reply | Threaded
Open this post in threaded view
|

Re: Using @SpringResource & @SpringApplicationContextResource in IgniteCallable

Hello!

I am trying to reproduce your issue and in my example everything is ok. It
looks like you create your spring application without igniteSpringBean. Look
at my example, please.

/////////////////////////////////////////////////////////////////////

public class Main {

    public static void main(String[] srgs) throws Exception {
        BeanFactory appCtx = new
ClassPathXmlApplicationContext("/spring-bean.xml");
        Ignite ignite = (Ignite) appCtx.getBean("igniteSpringBean");
        ignite.compute().call(C.getList(10));
    }

    static public class C implements IgniteCallable<Integer> {
        private static final long serialVersionUID = 4278959731940740185L;

        @IgniteInstanceResource
        private Ignite ignite;

        @SpringApplicationContextResource
        private ApplicationContext context;

        @Override
        public Integer call() throws Exception {
            System.out.println("Ignite: " + ignite);
            System.out.println("Context: " +
context.getBeanDefinitionNames());
            System.out.println("---------------");
            return 1;
        }

        public static List<C> getList(int size){
            return IntStream.range(0, size - 1).mapToObj(i -> new
C()).collect(Collectors.toList());
        }
    }
}
///////////////////////////////////////////////////////
// spring-bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">

    <bean id="beanToInject" class="java.lang.Integer">
        <constructor-arg value="111" type="int"/>
    </bean>
   
    <bean id="igniteSpringBean" class="org.apache.ignite.IgniteSpringBean">
        <property name="configuration">
            <bean id="ignite.cfg"
class="org.apache.ignite.configuration.IgniteConfiguration">
                <property name="peerClassLoadingEnabled" value="true"/>
               
                <property name="discoverySpi">
                    <bean
class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
                        <property name="ipFinder">
                            <bean
class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">
                                <property name="addresses">
                                    <list>
                                       
<value>127.0.0.1:47500..47509</value>
                                    </list>
                                </property>
                            </bean>
                        </property>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
</beans>




--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
ashishb888 ashishb888
Reply | Threaded
Open this post in threaded view
|

Re: Using @SpringResource & @SpringApplicationContextResource in IgniteCallable

Thank you Ilya for the response.

I have two application first one is the server and second one is client.
Here
<https://github.com/ashishb888/ignite-2.8-poc/tree/master/ignite-spring-resource>  
you can find the sever application code and  here
<https://github.com/ashishb888/ignite-2.8-poc/tree/master/ignite-spring-resource-compute-client>  
you can find the client application code

BR,
Ashish



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/