要使用ActiveMQ的经典Jakarta EE命名空间支持,需要进行以下步骤:
首先,确保已经安装了ActiveMQ,并设置好其相关配置。
在pom.xml文件中添加以下依赖项:
org.apache.activemq
activemq-jms-client
5.16.3
org.apache.activemq
activemq-broker
5.16.3
org.apache.activemq
activemq-core
5.16.3
import org.apache.activemq.ActiveMQConnectionFactory;
String brokerURL = "tcp://localhost:61616";
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURL);
import javax.jms.Connection;
import javax.jms.JMSException;
Connection connection = null;
try {
connection = connectionFactory.createConnection();
connection.start();
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
Session session = null;
MessageProducer producer = null;
MessageConsumer consumer = null;
try {
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(session.createQueue("queueName"));
consumer = session.createConsumer(session.createQueue("queueName"));
TextMessage message = session.createTextMessage("Hello, ActiveMQ!");
producer.send(message);
TextMessage receivedMessage = (TextMessage) consumer.receive();
System.out.println(receivedMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (producer != null) {
try {
producer.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
if (consumer != null) {
try {
consumer.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
if (session != null) {
try {
session.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
通过以上步骤,您可以使用ActiveMQ的经典Jakarta EE命名空间支持来创建和管理消息队列。请注意,上述代码只是一个简单的示例,您可能需要根据您的实际需求进行修改和扩展。
上一篇:ActiveMQ “Classic”支持“从客户端检测故障”吗?
下一篇:ActiveMQ-artemis地址属性中“Numberofmessages”和“Messagecount”的区别是什么?