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...

Installing GlassFish as a windows service

I would think installing GlassFish as a service is a fairly common requirement, however I battled to find any concise documentation on how to do this, so here's my bash.

I'm going to assume the following:
  • GlassFish is installed to c:\GlassFishv3
  • The GlassFish bin directory has been added to your path (c:\GlassFishv3\bin)
  • GlassFish 3.0.1
right now down to it, run the following statement:
asadmin create-service



















Easy enough, but I don't like the service name domain1 - as it is rather uninformative, I would much prefer a name like say GlassFish


cd glassfishv3\glassfish\domains\domain1\bin
notepad domain1Service.xml

This shows a file something like this



  domain1
  domain1 GlassFish Server
  GlassFish Server
  C:/glassfishv3/glassfish/bin/asadmin.bat
  C:/glassfishv3/glassfish/domains/domain1/bin
  reset
  tcpip
  start-domain
  --verbose
  --domaindir
  C:/glassfishv3/glassfish/domains
  domain1
  stop-domain
  --domaindir
  C:/glassfishv3/glassfish/domains
  domain1
The important elements for us are these


  domain1
  domain1 GlassFish Server
  GlassFish Server

Lets Change them to something more friendly like


  GlassFish 3.0.1
  GlassFish 3.0.1
  GlassFish 3.0.1 Server
Or whatever fits your needs.
Right now to round it off, run the thiese last 2 commands
Cd glassfish\3.0.1\glassfish\domains\domain1\bin
domain1Service.exe uninstall
domain1Service.exe install

You now have GlassFish installed with a service name of your choosing.