File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lazy initialize the JMS listener

 
Karan Jain
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:

One part of my application is listening to a topic for refreshing my cache.

But suppose the server which is publishing the message is down. I still want my application to come up.
I tried to set lazy-int = true and then after the context is loaded tried to do context.getBean("testUpdateListener") thinking it will initialize the bean and it will start listening.
But it does not work. The listener does not start listening.

What is the best way to achieve this?

 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, for listeners. Why aren't you using the jms namespace. So your config is as simple as



lazy-init has nothing to do with starting or shutting down a listener container. It is only about when Spring instantiates an object. And for me, I would never ever use lazy-init in my configuration. I want everything created up front.
It is better for memory, heap, also telling me about errors earlier which reduces my costs to find bugs.

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and you should also use the jee namespace for jndi lookups. It has a lookup tag

I personally would put the jndi environment values into a jndi.properties file and place it in the root of my classpath.

<jee:jndi-lookup id="connectionFactory" jndi-name="x.TopicFactory"/>

Mark
 
Karan Jain
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark for the response. I will try to implement using those namespaces.

But my requirement is to load the context even if my listener fails.

As there are other services which needs loading even the cache refresh does not work.

How can I achieve that?
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even with lazy init you would still have this problem.

If you don't have the listener, how will your application still be able to run. What if it still isn't up when a user needs it?

You could do some tricks, like parent child application contexts. Where you put yours listener into its own application context. When you create it in code, wrap it in a try-catch. Maybe even run that code in a separate thread that keeps running every 10 minutes until the listener is up, then after that application context is created stop running that job. And pass the other application context to the constructor to be the parent. Now it might need to be reversed because child application contexts can see beans in the parent, but not the other way around.

There might be another solution that I am not aware of.

Mark
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic