Friday, August 27, 2010

Kickstarting GlassFish Embedded JMS

We've started migrating our messaging onto GlassFish and its embedded messaging, and in the process discovered an oddity.

If you only use GlassFish for its JMS facilities, and don't have an EJB or web application that uses the JMS then GlassFish doesn't start it.

I've been told that in the next release you will be able to set the 'org.glassfish.jms.EagerStartup=true' system property which will start the JMS up at GlassFish start up.

But until then this is how i've found to get round it.

  1. Create a JMS Pool in GlassFish
  2. Create a dummy application that initializes the the embedded JMS

Here are the settings that I used in GlassFish 


Here is the Code for the little dummy application that boots the embedded JMS into action.
package sparg.tim;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.jms.ConnectionFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


@Singleton
@Startup
public class KickStartBean {

    @PostConstruct
    public void initMessaging(){
        try {
            Context context = new InitialContext();
            ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("JMSInitPool");
            Logger.getLogger(KickStartBean.class.getName()).log(Level.INFO,
                    "CF Systems: JMS HAS INITIALIZED");
        } catch (NamingException ex) {
            Logger.getLogger(KickStartBean.class.getName()).log(Level.SEVERE, 
                    "CF Systems: JMS HAS FAILED TO INITIALIZED", ex);
        }

    } 
}


voila! You now have a GlassFish appServer that automatically start its JMS. Now I eagerly await the new version that will allow me to take out this little hack...

No comments:

Post a Comment