首页 > 应用开发 > Java > ActiveMQ 入门及实例
MessageReceiver
package jms.activemq.myexample;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MessageReceiver implements Runnable {
private String url;
private String user;
private String password;
private final String QUEUE;
public MessageReceiver(String queue, String url, String user, String password) {
this.url = url;
this.user = user;
this.password = password;
this.QUEUE = queue;
}
@Override
public void run() {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
user, password, url);
Session session = null;
Destination receiveQueue;
try {
Connection connection = connectionFactory.createConnection();
session = connection
.createSession(true, Session.SESSION_TRANSACTED);
receiveQueue = session.createQueue(QUEUE);
MessageConsumer consumer = session.createConsumer(receiveQueue);
connection.start();
while (true) {
Message message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage receiveMessage = (TextMessage) message;
System.out.println("我是Receiver,收到消息如下: \r\n"
+ receiveMessage.getText());
} else {
session.commit();
break;
}
}
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
MessageSender
package jms.activemq.myexample;
import java.util.Date;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MessageSender implements Runnable {
private String url;
private String user;
private String password;
private final String QUEUE;
public MessageSender(String queue, String url, String user, String password) {
this.url = url;
this.user = user;
this.password = password;
this.QUEUE = queue;
}
@Override
public void run() {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
user, password, url);
Session session = null;
Destination sendQueue;
Connection connection = null;
int messageCount = 0;
try {
connection = connectionFactory.createConnection();
connection.start();
while (true) {
session = connection.createSession(true,
Session.SESSION_TRANSACTED);
sendQueue = session.createQueue(QUEUE);
MessageProducer sender = session.createProducer(sendQueue);
TextMessage outMessage = session.createTextMessage();
outMessage.setText(new Date() + "现在发送是第" + messageCount + "条消息");
sender.send(outMessage);
session.commit();
sender.close();
if ((++messageCount) == 10) {
// 发够十条消息退出
break;
}
Thread.sleep(1000);
}
connection.close();
} catch (JMSException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
MyActiveMQDemo
package jms.activemq.myexample;
public class MyActiveMQDemo {
public static void main(String[] args) {
String url = "tcp://localhost:61616";
String user = null;
String password = null;
String query = "MyQueue";
new Thread(new MessageSender(query,url,user,password), "Test-Sender").start();
new Thread(new MessageReceiver(query,url,user,password), "Test-Receiver").start();
}
}
可以把上面获得连接的通用部分提取出来,构造一个专门的类来管理
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;
}
}
所需jar包
activemq-all-5.6.0.jar
commons-logging-1.1.1.jar
log4j-1.2.17.jar
org.springframework.asm-3.1.0.RELEASE.jar
org.springframework.beans-3.1.0.RELEASE.jar
org.springframework.context-3.1.0.RELEASE.jar
org.springframework.core-3.1.0.RELEASE.jar
org.springframework.expression-3.1.0.RELEASE.jar
org.springframework.jms-3.1.0.RELEASE.jar
org.springframework.transaction-3.1.0.RELEASE.jar
spring配置文件applicationContext.xml
消息产生者
Java代码
package test;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.core.MessageCreator;
/**
* 消息产生者
* User
* Time: 12-6-14 上午11:31
*/
public class MyMessageCreator implements MessageCreator {
public int n = 0;
private static String str1 = "这个是第 ";
private static String str2 = " 个测试消息!";
private String str = "";
@Override
public Message createMessage(Session paramSession) throws JMSException {
System.out.println("MyMessageCreator n=" + n);
if (n == 9) {
//在这个例子中表示第9次调用时,发送结束消息
return paramSession.createTextMessage("end");
}
str = str1 + n + str2;
return paramSession.createTextMessage(str);
}
}
发送消息方
Java代码
package test;
import javax.jms.Destination;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
/**
* 发送消息方
* User
* Time: 12-6-14 上午11:29
*/
public class MessageSender extends Thread {
public static void main(String args[]) throws Exception {
String[] configLocations = new String[] {"test/applicationContext.xml"};
ApplicationContext context = new ClassPathXmlApplicationContext(configLocations);
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
Destination destination = (Destination) context.getBean("destination");
for (int i = 1; i < 100; i++) {
System.out.println("发送 i=" + i);
//消息产生者
MyMessageCreator myMessageCreator = new MyMessageCreator();
myMessageCreator.n = i;
jmsTemplate.send(destination, myMessageCreator);
sleep(10000);//10秒后发送下一条消息
}
}
}
消息接收方
Java代码
package test;
import javax.jms.Destination;
import javax.jms.TextMessage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
/**
* 消息接收方
* User
* Time: 12-6-14 上午11:32
*/
public class MessageReciver{
public static void main(String args[]) throws Exception {
String[] configLocations = new String[] {"test/applicationContext.xml"};
ApplicationContext context = new ClassPathXmlApplicationContext(configLocations);
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
Destination destination = (Destination) context.getBean("destination");
TextMessage msg = null;
//是否继续接收消息
boolean isContinue = true;
while (isContinue) {
msg = (TextMessage) jmsTemplate.receive(destination);
System.out.println("收到消息 :" + msg.getText());
if (msg.getText().equals("end")) {
isContinue = false;
System.out.println("收到退出消息,程序要退出!");
}
}
System.out.println("程序退出了!");
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9399028/viewspace-1815168/ ,如需转载,请注明出处,否则将追究法律责任。