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.







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
Which GF version ? OS ? You run all as default out Glassfish installer/package ?
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
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.
Pingback: Glassfish V3.1 running embedded ActiveMQ for JMS (Updates) | The JavaDude Weblog