Java Messaging System – Sending To Local Glassfish (v3)

Ok, so I’m still trying to get to grips with JMS. I’m sure it only took a couple of days to get MSMQ working, and even then that was due to some of the slightly more advanced topics. Here I’m still taking pigeon steps in the Hello, World equivalents of JMS.

But a minor break through last night was sending messages to a local Glassfish installation. The next step will be to try and handle error conditions locally, and then figure out how to call a remote Glassfish – I’m hoping these might come quite quickly, but with my previous experience of JMS I wouldn’t hold your breathe.

I think its important to say that I’m communicating with GlassFish v3 – I’ve read snippets that this is different from GlassFish v2 – although I’ve not personally tried at this stage.

1. Launch the GlassFish server and view the Admin Console.
2. Under Resources->JMS Resources->Connection Factories, create a new Connection Factory. Give the pool name a name of ‘jms/ConnectionFactory’, and set the type to ‘javax.jms.ConnectionFactory’.
3. Under JMS Destination Resource, create a new resource, setting the JNDI name to ‘jms/mqTest’, and physical name to ‘mqTest’ and the queue type to ‘javax,jms.Queue’.
(Note: If you chance any of these values afterwards, its worth restarting GlassFish. I got caught out by this last night).
4. Return to NetBeans, and create a new Java Application. In the Main.java enter the following code:

   @Resource(mappedName = "jms/ConnectionFactory")
   private static ConnectionFactory connectionFactory;
   @Resource(mappedName = "jms/mqTest")
   private static Queue queue;

   public static void main(String[] args) throws JMSException {
      MessageProducer messageProducer = null;
      TextMessage textMessage;
      Connection connection = null;
      Session session = null;

      try {
         connection = connectionFactory.createConnection();
         session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
         messageProducer = session.createProducer(queue);
         textMessage = session.createTextMessage();

         textMessage.setText("Hello, message queue");
         messageProducer.send(textMessage);
         System.out.println("Message sent?");
      } catch (Exception e) {
         System.out.println("Exception raised: " + e.getMessage());
      } finally {
         if (messageProducer != null)
            messageProducer.close();

         if (session != null)
            session.close();

         if (connection != null)
            connection.close();
      }
   }

5. You’ll need to locate the javax.jms.jar file – do a System search, but its located within the GlassFish installation area. Add it to your project.
6. Build the project from within NetBeans.
7. Locate the file appclient.bat (on a Windows machine at any rate) – again do a search, but should be located within the GlassFish installation area.
8. Go to a command line, and enter ‘appclient -jar <your compile jar name>’ (potentially needing to specify the full path to appclient.
9. Check the message has been sent by going to GlassFish Admin Console, Configuration->Java Messaging Service->Physical Destinations. Click the on View statistics link for your queue, and see that there a message waiting there.

In order to consume the message, see my previous entry (which will be revisited hopefully in the not too distant future).

Advertisements

3 thoughts on “Java Messaging System – Sending To Local Glassfish (v3)

  1. Pingback: Java Message Queuing (Continued) « Gregor Bowie's Blog

  2. In Eclipse, I get connectionFactory=null Exception. I have had some trouble with “@Resource” statement so perhaps that’s part of my problem. Anyway, if you can help this poor soul, that would be fantastic! Thanks for posting….

  3. Hi James,

    Just to check that you’re trying to talk to a local installation of GlassFish? Can you confirm that GlassFish has the conneciton factory in place? (Normally found under Resources->JMS Resources -> Connection Factories)?

    I’m also not sure how this would work with JMS 2.0 – a more recent blog post is here, but that is working within the GlassFish environment to send/receive messages: https://gregorbowie.wordpress.com/2013/09/24/jms-and-glassfish-4the-overview/

    Cheers,
    Greg

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s