1. Download ActiveMQ
and go to the official website to download: http://activemq.apache.org/
2. Run ActiveMQ
to decompress apache-activemq-5.5.1-bin.zip, double-click apache-activemq-5.5.1\bin\activemq .bat runs the ActiveMQ program.
After starting ActiveMQ, log in: http://localhost:8161/admin/, create a Queue and name it FirstQueue.
3. Import the jar package:
4. Example
1. Java code on the sender side
Collection code
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms. JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Sender {
/** Create Session */
public static Session createSession() {
// Connection factory, which JMS uses to create connections
ConnectionFactory connectionFactory = null;
// MS client to JMS Provider Connection
connection = null;
// A session that sends or receives messages
Session session = null;
try {
// Construct a ConnectionFactory instance object, where ActiveMq's implementation is used jar
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
// create a connection through the connection factory
connection = connectionFactory.createConnection();
// start the connection
connection.start();
// create a Session
session = connection.createSession( true, Session.AUTO_ACKNOWLEDGE);
} catch (Exception e) {
e.printStackTrace();
}
return session;
}
/** create message sender */
public static MessageProducer createProducer(Session session) {
// Destination of the message; to whom the message is sent.
Destination destination = null;
// Message sender
MessageProducer producer = null;
try {
// Get the session note parameter value FirstQueue is a server queue, which must be configured in ActiveMq's console
destination = session.createQueue("FirstQueue");
// Message--sender
producer = session.createProducer(destination);
// The setting is not persistent, it is actually decided according to the project
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
} catch ( Exception e) {
e.printStackTrace();
}
return producer;
}
/** 发送消息 */
public static void sendMessage(Session session, MessageProducer producer,
String content) {
TextMessage textMessage = null;
try {
textMessage = session.createTextMessage(content);
producer.send(textMessage);
session.commit();// 提交
} catch (JMSException e) {
e.printStackTrace();
}
}
/** 测试 */
public static void main(String[] args) {
Session session = createSession();
MessageProducer producer = createProducer(session);
for (int i = 0; i < 5; i++) {
String content = "Message---" + i;
System.out.println(content);
sendMessage(session, producer, content);
}
}
}
2、接收端
Java代码 收藏代码
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Receiver {
/** Create Session */
public static Session createSession() {
// Connection factory, JMS uses it to create connections
ConnectionFactory connectionFactory = null;
// MS client to JMS Provider Connection
connection = null;
// A session that sends or receives messages
Session session = null;
try {
// Construct a ConnectionFactory instance object, where ActiveMq's implementation is used jar
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
// create a connection through the connection factory
connection = connectionFactory.createConnection();
// start the connection
connection.start();
// create a Session
session = connection.createSession( true, Session.AUTO_ACKNOWLEDGE);
} catch (Exception e) {
e.printStackTrace();
}
return session;
}
/** create message receiver */
public static MessageConsumer createConsumer(Session session) {
// Destination of the message; to whom the message is sent.
Destination destination = null;
// Message receiver
MessageConsumer consumer = null;
try {
// Get the session note parameter value FirstQueue is a server queue, which must be configured in ActiveMq's console
destination = session.createQueue("FirstQueue");
// get the message producer "sender"
consumer = session.createConsumer(destination);
} catch (Exception e) {
e.printStackTrace();
}
return consumer;
}
/* * Send message */
public static void receiverMessage(MessageConsumer consumer) {
try {
while (true) {
// 设置接收者接收消息的时间
TextMessage message = (TextMessage) consumer.receive(6 * 1000);
if (null != message) {
System.out.println("ReceiverMessage--" + message.getText());
} else {
System.out.println("break");
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/** 测试 */
public static void main(String[] args) {
receiverMessage(createConsumer(createSession()));
}
}
5、测试结果
执行Sender:
Message---0
Message---1
Message---2
Message---3
Message---4
执行Receive:
ReceiverMessage--Message---0
ReceiverMessage--Message---1
ReceiverMessage--Message---2
ReceiverMessage--Message---3
ReceiverMessage--Message---4
Getting Started with ActiveMQ
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326484786&siteId=291194637
Ranking