Glassfish V3.1 running embedded ActiveMQ for JMS (Part 2)

This is just a POC Java SE application to show we can write to an embedded ActiveMQ queue using an external client.

Pre-Requirements:

  • Netbeans 6.5 +
  • Glassfish Setup from Tutorial Part 1

Tutorial:

  • Create Java Application

    New Java Application

    New Java Application

  • Add libraries from ActiveMQ package
    • activemq-all-5.5.0.jar
    • slf4j-log4j12-1.5.11
    • log4j-1.2.14

    Libraries

    Libraries

  • Change main code
    
    package simplesendamq;
    
    import javax.jms.Connection;
    import javax.jms.DeliveryMode;
    import javax.jms.Destination;
    import javax.jms.MessageProducer;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import org.apache.activemq.ActiveMQConnection;
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    public class SimpleSendAMQ {
    
        private String user = ActiveMQConnection.DEFAULT_USER;
        private String password = ActiveMQConnection.DEFAULT_PASSWORD;
        private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
        private Destination destination;
    
        public static void main(String[] args) {
            SimpleSendAMQ mySend = new SimpleSendAMQ();
            mySend.sendMessage();
        }
    
        void sendMessage() {
            try {
                Connection connection = null;
                ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
                connection = connectionFactory.createConnection();
                connection.start();
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                destination = session.createQueue("amqmsg");
                MessageProducer producer = session.createProducer(destination);
                producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    
                TextMessage message = session.createTextMessage("THIS IS THE TEST MESSAGE !");
                producer.send(message);
    
                connection.close();
    
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
    
    
  • Run and Check Queue

    Run

    Check Message

Most simple application to send to a queue, pretty much all default and hardcoded settings and we didn implement the MessageListener yet (which is not much more complicated in plain Java SE).

In Part 3 we look into creating a MDB (Message Driven Bean) to round up the tutorial. Stay tuned.

Remarks:
I have 2 feedbacks reaarding 404′s on the REST interface (http://localhost:4848/management/domain/resources/connector-resource/amqpool)
Not sure how you produce this. Please let me know if you have any Glassfish setup’s that are not default.

REST interface

About these ads

5 thoughts on “Glassfish V3.1 running embedded ActiveMQ for JMS (Part 2)

  1. Great tutorial, and everything works, but I have an “error” under Resources – JMS – Resources – Connection Factory – amqpool saying

    An error has occurred
    REST Request ‘http://localhost:4848/management/domain/resources/connector-resource/amqpool’ failed with response code ’404′.

    dont know if its important or not

      • Hi thanks for the quick replay and sorry for not including the version info in my post, so I am running

        ActiveMQ – same libraries that you specify

        Product Version: NetBeans IDE 7.0 (Build 201104080000)
        Java: 1.6.0_26; Java HotSpot(TM) 64-Bit Server VM 20.1-b02-384
        System: Mac OS X version 10.6.8 running on x86_64; MacRoman; en_US (nb)

        Glassfish is the included one in Netbeans 7: GlassFish Server Open Source Edition 3.1 (build 43)

        If you need any more info just drop a message, now I am of doing the third part, then I actually will start using JMS in a real project ;) perfect tutorial, great job

        cheers

  2. I noticed that in the sender you didn’t actually look-up/inject the ActiveMQ resources from GlassFish JNDI. Is that on purpose? In my case, JNDI look-ups to the ActiveMQ resources are failing.

  3. Pingback: Glassfish V3.1 running embedded ActiveMQ for JMS (Updates) | The JavaDude Weblog

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