File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Lazy initialize the JMS listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Lazy initialize the JMS listener" Watch "Lazy initialize the JMS listener" New topic
Author

Lazy initialize the JMS listener

Karan Jain
Ranch Hand

Joined: May 30, 2007
Posts: 82
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

Joined: May 30, 2007
Posts: 82
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lazy initialize the JMS listener