Sending a JMS message
Public classMymessageproducer {...//Create a connection factory instanceConnectionFactory connfactory =Newactivemqconnectionfactory (Activemqconnection.default_user, Activemqconnection.default_password , "tcp://localhost:61616"); Connection Conn=NULL; Try { //get Connection object instanceconn =connfactory.createconnection (); //Start ConnectionConn.start (); //To create a Session object instanceSession session = Conn.createsession (true, Session.auto_acknowledge); //Create a message destinationDestination Destination = Session.createqueue ("Hello_queue"); //Create a message producerMessageProducer Msgproducer =session.createproducer (destination); //Create a Message object instanceMessage textmsg = Session.createtextmessage ("This is a test message.")); //Send Messagemsgproducer.send (textmsg); //Submit a sessionSession.commit (); } Catch(jmsexception e) {e.printstacktrace (); } finally { //Close Connection if(Conn! =NULL) { Try{conn.close (); } Catch(jmsexception e) {e.printstacktrace (); } } } ...}
Receiving a JMS message synchronously
Public classMysynmessageconsumer {...//Create a connection factory instanceConnectionFactory connfactory =Newactivemqconnectionfactory (Activemqconnection.default_user, Activemqconnection.default_password , "tcp://localhost:61616"); Connection Conn=NULL; Try { //get Connection object instanceconn =connfactory.createconnection (); //Start ConnectionConn.start (); //To create a Session object instanceSession session = Conn.createsession (false, Session.auto_acknowledge); //Create a message destinationDestination Destination = Session.createqueue ("Hello_queue"); //Create message ConsumersMessageconsumer Msgconsumer =Session.createconsumer (destination); //Receiving MessagesTextMessage textmsg = (textmessage) msgconsumer.receive (10 * 1000); if(Textmsg! =NULL) {System.out.println (Textmsg.gettext ()); } } Catch(jmsexception e) {e.printstacktrace (); } finally { if(Conn! =NULL) { Try{conn.close (); } Catch(jmsexception e) {e.printstacktrace (); } } } ...}
Receiving a JMS message asynchronously
Public classMyasynmessageconsumer {...//Create a connection factory instanceConnectionFactory connfactory =Newactivemqconnectionfactory (Activemqconnection.default_user, Activemqconnection.default_password , "tcp://localhost:61616"); Connection Conn=NULL; Try { //get Connection object instanceconn =connfactory.createconnection (); //Start ConnectionConn.start (); //To create a Session object instanceSession session = Conn.createsession (false, Session.auto_acknowledge); //Create a message destinationDestination Destination = Session.createqueue ("Hello_queue"); //Create message ConsumersMessageconsumer Msgconsumer =Session.createconsumer (destination); //registering a message listenerMsgconsumer.setmessagelistener (NewMessageListener () {@Override Public voidonMessage (Message msg) {textmessage textmsg=(TextMessage) msg; Try{System.out.println (Textmsg.gettext ()); } Catch(jmsexception e) {e.printstacktrace (); } } }); Thread.Sleep (60 * 1000); } Catch(jmsexception e) {e.printstacktrace (); } Catch(interruptedexception e) {e.printstacktrace (); } finally { //Close Connection if(Conn! =NULL) { Try{conn.close (); } Catch(jmsexception e) {e.printstacktrace (); } } } ...}
Others
- For the PUB/SUB model, use the Session.createtopic method to create the Destination.
- Messageconsumer the Receive () method blocks the thread until it receives the next message while synchronizing the consumption message, the receive (long Timeout) method blocks the thread for the specified time until it receives the next message, and returns a null value if it times out The Receivenowait () method immediately receives the next message and returns a null value if there is no message in the source.
ActiveMQ (5.10.0)-Hello World