ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现
贴代码:
server连接方式配置文件: conection.properties
#服务器连接地址
url=tcp://192.168.1.101:61616
#消息队列名
queue=MYQUEUE
#授权用户
username=user
#授权密码
password=password
server连接管理类:ConnectionManager.java
package net.itant.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 管理服务器连接
*
*/
public class ConnectionManager {
private String url; //远程服务器地址
private String queue; //消息队列名
private String username; //用于连接服务器的用户名
private String password; //用于连接服务器的密码
private final String URL = "url"; //properties文件中url的key
private final String QUEUE = "queue"; //properties文件中query的key
private final String USERNAME = "username"; //properties文件中username的key
private final String PASSWORD = "password"; //properties文件中password的key
private final String FILEPATH = "/connection.properties";
/**
* 初始化连接字符串
* @return 是否初始化成功
*/
public Boolean init() {
Properties properties = new Properties();
InputStream inputStream = Object.class.getResourceAsStream(FILEPATH);
if (inputStream != null) {
try {
properties.load(inputStream);
url = properties.getProperty(URL);
queue = properties.getProperty(QUEUE);
username = properties.getProperty(USERNAME);
password = properties.getProperty(PASSWORD);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
} else {
}
return false;
}
/**
* 与远程服务器取得连接
* @param url 服务器连接地址
* @param username 用户名
* @param password 密码
* @return 连接对象
* @throws JMSException JMS异常S
*/
public Connection getConnection() {
Connection connection = null;
if (init()) {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
try {
connection = factory.createConnection(username, password);
} catch (JMSException e) {
e.printStackTrace();
}
}
return connection;
}
/**
* 关闭连接
* @param connection 与远程服务器连接
* @throws JMSException JMS异常
*/
public void closeConnection(Connection connection, Session session,
MessageProducer producer, MessageConsumer consumer) {
try {
if (producer != null) {
producer.close();
}
if (consumer != null) {
consumer.close();
}
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
} catch (JMSException e) {
e.printStackTrace();
}
}
/**
* 取得队列名
* @return 队列名
*/
public String getQueue() {
init();
return queue;
}
}
消息处理类:DataExchange.java
package net.itant.util;
import java.util.ArrayList;
import java.util.List;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
/**
* 与远程服务器通过JMS进行数据传递
*
*/
public class DataExchange {
/**
* 往消息队列发送消息
* @param data 消息内容
*/
public void dataSend(String data) {
Connection connection;
Session session = null;
MessageProducer producer = null;
String queue;
ConnectionManager manager = new ConnectionManager();
connection = manager.getConnection();
try {
connection.start();
queue = manager.getQueue();
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(queue);
producer = session.createProducer(destination);
TextMessage message = session.createTextMessage(data);
producer.send(message);
session.commit();
} catch (JMSException e) {
e.printStackTrace();
} finally {
manager.closeConnection(connection, session, producer, null);
}
}
/**
* 从服务器消息队列取消息
* @param timeout 超时时间
* @return 取到的消息
*/
public List<String> dataReceive() {
List<String> resultList = new ArrayList<String>();
final int TIMEOUT = 1000;
Connection connection;
Session session = null;
MessageConsumer consumer = null;
String queue;
ConnectionManager manager = new ConnectionManager();
connection = manager.getConnection();
queue = manager.getQueue();
try {
connection = manager.getConnection();
connection.start();
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(queue);
consumer = session.createConsumer(destination);
// Receives the next message that arrives within the specified timeout interval
Message message;
while ((message = consumer.receive(TIMEOUT)) != null){
if (message != null && (message instanceof TextMessage) ) {
TextMessage txtMsg = (TextMessage) message;
resultList.add(txtMsg.getText());
}
}
session.commit();
} catch (JMSException e){
e.printStackTrace();
} finally {
manager.closeConnection(connection, session, null, consumer);
}
return resultList;
}
}