Quantcast
Viewing all articles
Browse latest Browse all 14

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

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

  • 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

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.


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 14

Trending Articles