ActiveMQ5.2
All the packages used are included in ActiveMQ. The referenced package is shown in the following figure:
PackageStujms. p2ptxt; ImportOrg. springframework. context. ApplicationContext; ImportOrg. springframework. context. support. ClassPathXmlApplicationContext; ImportOrg. springframework. jms. core. JmsTemplate; ImportOrg. springframework. jms. core. MessageCreator; ImportJavax. jms. Destination; ImportJavax. jms. JMSException; ImportJavax. jms. Message; ImportJavax. jms. Session; /** * Message sender * * @ Author leizhimin 2009-8-13 17:01:48 */ Public ClassMySender { Public Static VoidMain (String [] args ){ ApplicationContext ctx =NewClassPathXmlApplicationContext ("/ApplicationContext. xml"); JmsTemplate template = (JmsTemplate) ctx. getBean ("JmsTemplate"); Destination destination = (Destination) ctx. getBean ("Destination"); Template. send (destination,NewMessageCreator (){ PublicMessage createMessage (Session session)ThrowsJMSException { ReturnSession. createTextMessage ("Send Message: Hello ActiveMQ Text Message! "); } }); System. out. println ("A jms message is successfully sent"); } } |
PackageStujms. p2ptxt; ImportOrg. springframework. context. ApplicationContext; ImportOrg. springframework. context. support. ClassPathXmlApplicationContext; ImportOrg. springframework. jms. core. JmsTemplate; ImportJavax. jms. Destination; ImportJavax. jms. JMSException; ImportJavax. jms. TextMessage; /** * Message recipient * * @ Author leizhimin 2009-8-13 17:02:04 */ Public ClassMycycler { Public Static VoidMain (String [] args)ThrowsJMSException { ApplicationContext ctx =NewClassPathXmlApplicationContext ("/ApplicationContext. xml"); JmsTemplate template = (JmsTemplate) ctx. getBean ("JmsTemplate"); Destination destination = (Destination) ctx. getBean ("Destination"); While (True){ TextMessage txtmsg = (TextMessage) template. receive (destination ); If (Null! = Txtmsg) System. out. println ("Received message content :"+ Txtmsg. getText ()); Else Break; } } } |
<?Xml Version= "1.0" Encoding= "UTF-8"?> <Beans Xmlns= "Http://www.springframework.org/schema/beans" Xmlns: xsi= "Http://www.w3.org/2001/XMLSchema-instance" Xmlns: context= "Http://www.springframework.org/schema/context" Xsi: schemaLocation = "http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <! --Configure the JMS connection factory--> <Bean Id= "ConnectionFactory" Class= "Org. apache. activemq. spring. ActiveMQConnectionFactory"> <Property Name= "BrokerURL" Value= "Tcp: // localhost: 61616"/> </Bean> <! --Configure the JMS Template--> <Bean Id= "JmsTemplate" Class= "Org. springframework. jms. core. JmsTemplate"> <Property Name= "ConnectionFactory" Ref= "ConnectionFactory"/> </Bean> <! --Message sending destination (one queue)--> <Bean Id= "Destination" Class= "Org. apache. activemq. command. ActiveMQQueue"> <! --Set the name of the message queue--> <Constructor-arg Index= "0" Value= "HelloWorldQueue"/> </Bean> </Beans> |
Run the sender three times:
A jms message is sent successfully. Process finished with exit code 0 |
Run the acceptor again:
The Message content is: Send Message: Hello ActiveMQ Text Message! The Message content is: Send Message: Hello ActiveMQ Text Message! The Message content is: Send Message: Hello ActiveMQ Text Message! |
ContinueTestIt is found that the receiving end does not exit the program after receiving a message, but continues to wait. Once a message is sent, it is obtained and output!
Send a picture to see: