創建ActiveMQ連接:
import JAVAx.jms.*;
import org.Apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQExample {
public static void mAIn(String[] args) {
try {
// 創建連接工廠
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 創建連接
Connection connection = factory.createConnection();
// 啟動連接
connection.start();
// 關閉連接
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
創建ActiveMQ會話:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQExample {
public static void main(String[] args) {
try {
// 創建連接工廠
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 創建連接
Connection connection = factory.createConnection();
// 創建會話
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 關閉會話
session.close();
// 關閉連接
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
創建ActiveMQ隊列:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQExample {
public static void main(String[] args) {
try {
// 創建連接工廠
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 創建連接
Connection connection = factory.createConnection();
// 創建會話
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 創建隊列
Queue queue = session.createQueue("myQueue");
// 關閉會話
session.close();
// 關閉連接
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
創建ActiveMQ主題:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQExample {
public static void main(String[] args) {
try {
// 創建連接工廠
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 創建連接
Connection connection = factory.createConnection();
// 創建會話
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 創建主題
Topic topic = session.createTopic("myTopic");
// 關閉會話
session.close();
// 關閉連接
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
發送消息到隊列:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQExample {
public static void main(String[] args) {
try {
// 創建連接工廠
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 創建連接
Connection connection = factory.createConnection();
// 創建會話
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 創建隊列
Queue queue = session.createQueue("myQueue");
// 創建消息生產者
MessageProducer producer = session.createProducer(queue);
// 創建消息
TextMessage message = session.createTextMessage("Hello, ActiveMQ!");
// 發送消息
producer.send(message);
// 關閉會話
session.close();
// 關閉連接
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
發送消息到主題:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQExample {
public static void main(String[] args) {
try {
// 創建連接工廠
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 創建連接
Connection connection = factory.createConnection();
// 創建會話
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 創建主題
Topic topic = session.createTopic("myTopic");
// 創建消息生產者
MessageProducer producer = session.createProducer(topic);
// 創建消息
TextMessage message = session.createTextMessage("Hello, ActiveMQ!");
// 發送消息
producer.send(message);
// 關閉會話
session.close();
// 關閉連接
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
接收隊列中的消息:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQExample {
public static void main(String[] args) {
try {
// 創建連接工廠
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 創建連接
Connection connection = factory.createConnection();
// 創建會話
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 創建隊列
Queue queue = session.createQueue("myQueue");
// 創建消息消費者
MessageConsumer consumer = session.createConsumer(queue);
// 接收消息
Message message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message: " + textMessage.getText());
}
// 關閉會話
session.close();
// 關閉連接
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
接收主題中的消息:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQExample {
public static void main(String[] args) {
try {
// 創建連接工廠
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 創建連接
Connection connection = factory.createConnection();
// 創建會話
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 創建主題
Topic topic = session.createTopic("myTopic");
// 創建消息消費者
MessageConsumer consumer = session.createConsumer(topic);
// 接收消息
Message message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message: " + textMessage.getText());
}
// 關閉會話
session.close();
// 關閉連接
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
發布-訂閱模式中的發布者:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQExample {
public static void main(String[] args) {
try {
// 創建連接工廠
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 創建連接
Connection connection = factory.createConnection();
// 創建會話
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 創建主題
Topic topic = session.createTopic("myTopic");
// 創建消息生產者
MessageProducer producer = session.createProducer(topic);
// 創建消息
TextMessage message = session.createTextMessage("Hello, ActiveMQ!");
// 發送消息
producer.send(message);
// 關閉會話
session.close();
// 關閉連接
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
發布-訂閱模式中的訂閱者:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQExample {
public static void main(String[] args) {
try {
// 創建連接工廠
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 創建連接
Connection connection = factory.createConnection();
// 創建會話
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 創建主題
Topic topic = session.createTopic("myTopic");
// 創建消息消費者
MessageConsumer consumer = session.createConsumer(topic);
// 接收消息
Message message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message: " + textMessage.getText());
}
// 關閉會話
session.close();
// 關閉連接
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
使用消息監聽器接收隊列中的消息:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQExample {
public static void main(String[] args) {
try {
// 創建連接工廠
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 創建連接
Connection connection = factory.createConnection();
// 創建會話
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 創建隊列
Queue queue = session.createQueue("myQueue");
// 創建消息消費者
MessageConsumer consumer = session.createConsumer(queue);
// 注冊消息監聽器
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("Received message: " + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
});
// 啟動連接
connection.start();
// 等待接收消息
Thread.sleep(5000);
// 關閉會話
session.close();
// 關閉連接
connection.close();
} catch (JMSException | InterruptedException e) {
e.printStackTrace();
}
}
}
使用消息監聽器接收主題中的消息:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQExample {
public static void main(String[] args) {
try {
// 創建連接工廠
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 創建連接
Connection connection = factory.createConnection();
// 創建會話
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 創建主題
Topic topic = session.createTopic("myTopic");
// 創建消息消費者
MessageConsumer consumer = session.createConsumer(topic);
// 注冊消息監聽器
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("Received message: " + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
});
// 啟動連接
connection.start();
// 等待接收消息
Thread.sleep(5000);
// 關閉會話
session.close();
// 關閉連接
connection.close();
} catch (JMSException | InterruptedException e) {
e.printStackTrace();
}
}
}
使用消息選擇器接收隊列中的消息:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQExample {
public static void main(String[] args) {
try {
// 創建連接工廠
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 創建連接
Connection connection = factory.createConnection();
// 創建會話
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 創建隊列
Queue queue = session.createQueue("myQueue");
// 創建消息消費者
MessageConsumer consumer = session.createConsumer(queue, "age > 18");
// 接收消息
Message message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message: " + textMessage.getText());
}
// 關閉會話
session.close();
// 關閉連接
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
使用消息選擇器接收主題中的消息:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQExample {
public static void main(String[] args) {
try {
// 創建連接工廠
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 創建連接
Connection connection = factory.createConnection();
// 創建會話
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 創建主題
Topic topic = session.createTopic("myTopic");
// 創建消息消費者
MessageConsumer consumer = session.createConsumer(topic, "age > 18");
// 接收消息
Message message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message: " + textMessage.getText());
}
// 關閉會話
session.close();
// 關閉連接
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
使用事務發送消息:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQExample {
public static void main(String[] args) {
try {
// 創建連接工廠
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 創建連接
Connection connection = factory.createConnection();
// 創建會話
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
// 創建隊列
Queue queue = session.createQueue("myQueue");
// 創建消息生產者
MessageProducer producer = session.createProducer(queue);
// 創建消息
TextMessage message = session.createTextMessage("Hello, ActiveMQ!");
// 發送消息
producer.send(message);
// 提交事務
session.commit();
// 關閉會話
session.close();
// 關閉連接
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
這些示例覆蓋了ActiveMQ的常用用法,包括創建連接、創建會話、創建隊列和主題、發送和接收消息等。你可以根據自己的需求選擇相應的示例進行參考和使用。