Required to introduce the JAR package:
Jms-1.1.jar
Activemq-all-5.15.0.jar
Producers
PackageCom.mousewheel.demo;Importjavax.jms.Connection;Importjavax.jms.ConnectionFactory;Importjavax.jms.Destination;Importjavax.jms.JMSException;ImportJavax.jms.Message;ImportJavax.jms.MessageConsumer;ImportJavax.jms.MessageListener;ImportJavax.jms.ObjectMessage;Importjavax.jms.Session;Importorg.apache.activemq.ActiveMQConnectionFactory; Public classMqconsumerdemo { Public Static voidMain (string[] args)throwsjmsexception {connectionfactory connectionfactory=NewActivemqconnectionfactory ("admin", "admin", "tcp://192.168.31.33:61616"); Connection Connection=NULL; Try{Connection=connectionfactory.createconnection (); Connection.start (); Session Session= Connection.createsession (false, Session.auto_acknowledge); Destination Destination= Session.createqueue ("Test-queue"); Messageconsumer Consumer=Session.createconsumer (destination); Consumer.setmessagelistener (NewMessageListener () {@Override Public voidonMessage (Message message) {Try{Mqbean Bean=( Mqbean) ((objectmessage) message). GetObject (); System.out.println (Bean); if(NULL!=message) {System.out.println ("Receive Message" +bean.getname ()); } } Catch(Exception e) {//Todo:handle Exception } } }); System.out.println ("Begin Listen"); } Catch(Exception e) {//Todo:handle Exception } }}
Consumers
PackageCom.mousewheel.demo;Importjavax.jms.Connection;Importjavax.jms.ConnectionFactory;Importjavax.jms.Destination;Importjavax.jms.JMSException;ImportJavax.jms.Message;ImportJavax.jms.MessageConsumer;ImportJavax.jms.MessageListener;ImportJavax.jms.ObjectMessage;Importjavax.jms.Session;Importorg.apache.activemq.ActiveMQConnectionFactory; Public classMqconsumerdemo { Public Static voidMain (string[] args)throwsjmsexception {connectionfactory connectionfactory=NewActivemqconnectionfactory ("admin", "admin", "tcp://192.168.31.33:61616"); Connection Connection=NULL; Try{Connection=connectionfactory.createconnection (); Connection.start (); Session Session= Connection.createsession (false, Session.auto_acknowledge); Destination Destination= Session.createqueue ("Test-queue"); Messageconsumer Consumer=Session.createconsumer (destination); Consumer.setmessagelistener (NewMessageListener () {@Override Public voidonMessage (Message message) {Try{Mqbean Bean=( Mqbean) ((objectmessage) message). GetObject (); System.out.println (Bean); if(NULL!=message) {System.out.println ("Receive Message" +bean.getname ()); } } Catch(Exception e) {//Todo:handle Exception } } }); System.out.println ("Begin Listen"); } Catch(Exception e) {//Todo:handle Exception } }}
Java ActiveMQ Example