介绍下JMS通信: 首先下载ActiveMQ http://activemq.apache.org/ 以下是Client和Server的代码实现:
Client代码:

package
com.jms.test;


import
javax.jms.Connection;

import
javax.jms.DeliveryMode;

import
javax.jms.Destination;

import
javax.jms.JMSException;

import
javax.jms.Message;

import
javax.jms.MessageConsumer;

import
javax.jms.MessageListener;

import
javax.jms.MessageProducer;

import
javax.jms.Session;

import
javax.jms.TextMessage;


import
org.apache.activemq.ActiveMQConnectionFactory;


public
class
Client
implements
MessageListener
{

private
String url
=
”
tcp://127.0.0.1:61616
”
;


protected
Connection createConnection()
throws
JMSException, Exception
{

ActiveMQConnectionFactory connectionFactory
=
new
ActiveMQConnectionFactory(

url);

Connection connection
=
connectionFactory.createConnection();


connection.start();

return
connection;

}


protected
Session createSession(Connection connection)
throws
Exception
{

Session session
=
connection.createSession(
false
,

Session.AUTO_ACKNOWLEDGE);

Client代码:

package
com.jms.test;


import
javax.jms.Connection;

import
javax.jms.DeliveryMode;

import
javax.jms.Destination;

import
javax.jms.JMSException;

import
javax.jms.Message;

import
javax.jms.MessageConsumer;

import
javax.jms.MessageListener;

import
javax.jms.MessageProducer;

import
javax.jms.Session;

import
javax.jms.TextMessage;


import
org.apache.activemq.ActiveMQConnectionFactory;


public
class
Client
implements
MessageListener
{

private
String url
=
”
tcp://127.0.0.1:61616
”
;


protected
Connection createConnection()
throws
JMSException, Exception
{

ActiveMQConnectionFactory connectionFactory
=
new
ActiveMQConnectionFactory(

url);

Connection connection
=
connectionFactory.createConnection();


connection.start();

return
connection;

}


protected
Session createSession(Connection connection)
throws
Exception
{

Session session
=
connection.createSession(
false
,

Session.AUTO_ACKNOWLEDGE);

版权声明:本文为rockyd原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。