You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by Humphrey <hm...@gmail.com> on 2017/07/10 14:52:03 UTC

Using AbstractAnnotationConfigDispatcherServletInitializer

Hello,

Here [1] there is an explanation how to startup Ignite in an WAR (Spring
MVC) file using the web.xml.



I managed to get it started with the following code:



I would like to have Ignite started programmatically, without having a
spring based configuration file "config/default-config.xml" file, but by
specifying a IgniteConfiguration and CacheConfiguration in Java Code.

I tried to have it started with the following but it's not getting started:


Is it possible to start Ignite programmatically when the WAR is starting up
(by loading that bean)?

[1]
https://apacheignite-mix.readme.io/v1.9/docs/web-session-clustering#configuration



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Using-AbstractAnnotationConfigDispatcherServletInitializer-tp14592.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: Using AbstractAnnotationConfigDispatcherServletInitializer

Posted by afedotov <al...@gmail.com>.
Hi,

IgniteSpringBean encapsulates lifecycle management and in general provides
a more convenient way of Ignite instance creation. For more details please
refer to the Java docs of the class.

Kind regards,
Alex

10 июля 2017 г. 8:05 PM пользователь "Humphrey [via Apache Ignite Users]" <
ml+s70518n14600h34@n6.nabble.com> написал:

> Thanks, actually I got both ways working.
> But I prefer to use the IgniteSpringBean.
>
> In my DAO where Ignite is being used, I implemented the interfaces
> InitializingBean and DisposableBean and used the IgniteSpringBean to get my
> Cache. Now when my DAO is being initialized it initializes the
> IgniteSpringBean as well.
>
> What is so special about the IgniteSpringBean vs a spring bean instance of
> Ignition.ignite()? In the spring application context there will always be
> one instance of the "ignite" bean (singleton). So all DAO who uses
> @Autowired will receive the same instance of Ignite right?
>
>
> @Configuration
> @EnableWebMvc
> @ComponentScan(basePackages = {"mypackage"})
> public class AppConfig {
>
> 	@Bean
> 	public IgniteSpringBean igniteInstance() {
> 		IgniteSpringBean ignite = new IgniteSpringBean();
> 		ignite.setConfiguration(getIgniteConfiguration());
> 		return ignite;
> 	}
> ...
>
>
> And my DAO
>
>
> @Component
> public class myDAO implements InitializingBean, DisposableBean, AutoCloseable {
> 	
> 	@Autowired
> 	private IgniteSpringBean ignite;
> 	....
> 	....
>
> 	@Override
> 	public void afterPropertiesSet() throws Exception {
> 		myCache = ignite.cache(MY_CACHE_NAME);
> 		sequence = ignite.atomicSequence(MY_CACHE_NAME, 1, true);
> 	}
>
> 	@Override
> 	public void destroy() throws Exception {
> 		close();
> 	}
>
>
> Before I had the methods now in the afterPropertiesSet in the constructor
> of the Dao which gave error on startup. Now I have the constructor empty.
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
> http://apache-ignite-users.70518.x6.nabble.com/Using-
> AbstractAnnotationConfigDispatcherServletInitializer-tp14592p14600.html
> To start a new topic under Apache Ignite Users, email
> ml+s70518n1h65@n6.nabble.com
> To unsubscribe from Apache Ignite Users, click here
> <http://apache-ignite-users.70518.x6.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=1&code=YWxleGFuZGVyLmZlZG90b2ZmQGdtYWlsLmNvbXwxfC0xMzYxNTU0NTg=>
> .
> NAML
> <http://apache-ignite-users.70518.x6.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Using-AbstractAnnotationConfigDispatcherServletInitializer-tp14592p14621.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: Using AbstractAnnotationConfigDispatcherServletInitializer

Posted by Humphrey <hm...@gmail.com>.
Thanks, actually I got both ways working. 
But I prefer to use the IgniteSpringBean.

In my DAO where Ignite is being used, I implemented the interfaces
InitializingBean and DisposableBean and used the IgniteSpringBean to get my
Cache. Now when my DAO is being initialized it initializes the
IgniteSpringBean as well.

What is so special about the IgniteSpringBean vs a spring bean instance of
Ignition.ignite()? In the spring application context there will always be
one instance of the "ignite" bean (singleton). So all DAO who uses
@Autowired will receive the same instance of Ignite right?



And my DAO



Before I had the methods now in the afterPropertiesSet in the constructor of
the Dao which gave error on startup. Now I have the constructor empty.




--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Using-AbstractAnnotationConfigDispatcherServletInitializer-tp14592p14600.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: Using AbstractAnnotationConfigDispatcherServletInitializer

Posted by afedotov <al...@gmail.com>.
Hi,

Actually, it should have worked. Try adding a call to
ignite.afterPropertiesSet() in IgniteSpringBean definition method.

An example of a configuration that works is as follows:

@Configuration
public class IgniteConfig {

    @Bean
    public IgniteSpringBean igniteSpringBean() {
        IgniteSpringBean bean = new IgniteSpringBean();

        bean.setConfiguration(igniteConfiguration());

        return bean;
    }

    public IgniteConfiguration igniteConfiguration() {
        IgniteConfiguration cfg = new IgniteConfiguration();

        //cfg.setClientMode(true);
        cfg.setGridName("igniteInstanceName");
        cfg.setPeerClassLoadingEnabled(true);
        cfg.setMetricsLogFrequency(0);

        TcpCommunicationSpi commSpi = new TcpCommunicationSpi();

        commSpi.setIdleConnectionTimeout(400000);

        cfg.setCommunicationSpi(commSpi);

        TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();

        ipFinder.setAddresses(Collections.singletonList("localhost:47500..47509"));

        TcpDiscoverySpi discoverySpi = new TcpDiscoverySpi();

        discoverySpi.setIpFinder(ipFinder);

        cfg.setDiscoverySpi(discoverySpi);

        return cfg;
    }
}


Kind regards,
Alex.

On Mon, Jul 10, 2017 at 6:50 PM, Humphrey [via Apache Ignite Users] <
ml+s70518n14596h10@n6.nabble.com> wrote:

> Thanks for the reply:
>
> I tried the following and was not succesfull.
>
> @Configuration
> @EnableWebMvc
> @ComponentScan(basePackages = {"mypackage"})
> public class AppConfig {
>
> 	@Bean
> 	public IgniteSpringBean igniteInstance() {
> 		IgniteSpringBean ignite = new IgniteSpringBean();
> 		ignite.setConfiguration(getIgniteConfiguration());
> 		return ignite;
> 	}
> 	
> 	private IgniteConfiguration getIgniteConfiguration() {
> 		IgniteConfiguration cfg = new IgniteConfiguration();
> 		......
>
>
> Should it be added somewhere else? Is there an example of this?
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
> http://apache-ignite-users.70518.x6.nabble.com/Using-
> AbstractAnnotationConfigDispatcherServletInitializer-tp14592p14596.html
> To start a new topic under Apache Ignite Users, email
> ml+s70518n1h65@n6.nabble.com
> To unsubscribe from Apache Ignite Users, click here
> <http://apache-ignite-users.70518.x6.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=1&code=YWxleGFuZGVyLmZlZG90b2ZmQGdtYWlsLmNvbXwxfC0xMzYxNTU0NTg=>
> .
> NAML
> <http://apache-ignite-users.70518.x6.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Using-AbstractAnnotationConfigDispatcherServletInitializer-tp14592p14599.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: Using AbstractAnnotationConfigDispatcherServletInitializer

Posted by Humphrey <hm...@gmail.com>.
Thanks for the reply:

I tried the following and was not succesfull.


Should it be added somewhere else? Is there an example of this?




--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Using-AbstractAnnotationConfigDispatcherServletInitializer-tp14592p14596.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: Using AbstractAnnotationConfigDispatcherServletInitializer

Posted by afedotov <al...@gmail.com>.
Hi,

Please check if IgniteSpringBean will help in your case.

Kind regards,
Alex.

On Mon, Jul 10, 2017 at 5:52 PM, Humphrey [via Apache Ignite Users] <
ml+s70518n14592h86@n6.nabble.com> wrote:

> Hello,
>
> Here [1] there is an explanation how to startup Ignite in an WAR (Spring
> MVC) file using the web.xml.
>
> <listener>
>    <listener-class>org.apache.ignite.startup.servlet.ServletContextListenerStartup</listener-class>
> </listener>
>
> <context-param>
>    <param-name>IgniteConfigurationFilePath</param-name>
>    <param-value>config/default-config.xml </param-value>
> </context-param>
>
>
> I managed to get it started with the following code:
>
> 	@Override
> 	public void onStartup(ServletContext servletContext) throws ServletException {
> 		
> 		servletContext.addListener(ServletContextListenerStartup.class);
> 		servletContext.setInitParameter(ServletContextListenerStartup.IGNITE_CFG_FILE_PATH_PARAM, "config/default-config.xml");
> 		
> 		super.onStartup(servletContext);
> 	}
>
>
> I would like to have Ignite started programmatically, without having a
> spring based configuration file "config/default-config.xml" file, but by
> specifying a IgniteConfiguration and CacheConfiguration in Java Code.
>
> I tried to have it started with the following but it's not getting
> started:
>
> @Configuration
> @EnableWebMvc
> @ComponentScan(basePackages = {"mypackage"})
>
> public class AppConfig {
>
> 	@Bean
> 	public Ignite igniteInstance() {
> 		IgniteConfiguration cfg = new IgniteConfiguration();
> 		cfg.setClientMode(true);
> 		cfg.setPeerClassLoadingEnabled(true);
> 		return Ignition.start(cfg);
> 	}
> ....
>
>
> Is it possible to start Ignite programmatically when the WAR is starting
> up (by loading that bean)?
>
> [1] https://apacheignite-mix.readme.io/v1.9/docs/web-session-clustering#
> configuration
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
> http://apache-ignite-users.70518.x6.nabble.com/Using-
> AbstractAnnotationConfigDispatcherServletInitializer-tp14592.html
> To start a new topic under Apache Ignite Users, email
> ml+s70518n1h65@n6.nabble.com
> To unsubscribe from Apache Ignite Users, click here
> <http://apache-ignite-users.70518.x6.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=1&code=YWxleGFuZGVyLmZlZG90b2ZmQGdtYWlsLmNvbXwxfC0xMzYxNTU0NTg=>
> .
> NAML
> <http://apache-ignite-users.70518.x6.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Using-AbstractAnnotationConfigDispatcherServletInitializer-tp14592p14594.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.