使用 JMS 向 RabbitMQ 发送消息
Sending message to RabbitMQ using JMS
我是 RabbitMQ 的新手,正在尝试使用 JMS 向 RabbitMQ 发送消息。我有以下标准 JMS 生产者程序:
public void sendMessage() {
Context context = null;
ConnectionFactory factory = null;
Destination destination = null;
Connection connection = null;
Session session = null;
MessageProducer producer = null;
Properties initialProperties = new Properties();
initialProperties.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
initialProperties.put(InitialContext.PROVIDER_URL, "http-remoting://IP:PORT");
initialProperties.put(Context.SECURITY_PRINCIPAL, "mquser");
initialProperties.put(Context.SECURITY_CREDENTIALS, "Mquser@123");
try {
context = new InitialContext(initialProperties);
factory = (QueueConnectionFactory) context.lookup("jms/RemoteConnectionFactory");
System.out.println("Lookup Success");
destination = (Destination) context.lookup("jms/queue/TestQueue");
System.out.println("Queue lookup success");
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination);
String text = "8=FIX.4.49=7735=349=A56=B34=352=20200115-13:18:26.000 45=322222=D100208103222223=40558=Invalid FIX2ITFMsgType10=226";
TextMessage textMessage = session.createTextMessage();
textMessage.setText(text);
connection.start();
System.out.println("Going to send");
producer.send(textMessage);
System.out.println(this.getClass().getName() + "has sent a message : " + text);
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (context != null) {
try {
context.close();
} catch (NamingException ex) {
ex.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (JMSException ex) {
ex.printStackTrace();
}
}
}
}
我正在使用它向 JBoss EAP 7.2 中嵌入的 ActiveMQ Artemis 发送消息。正如我在 this link 中发现的那样,我将无法使用上述程序向 RabbitMQ 发送消息,但可以使用以下参数:
- 用户名
- 密码
- 虚拟主机
- 主持人
- 端口
- 目的地名称
- 交易所名称
- 队列名称
- 路由键
谁能给我一个向 RabbitMQ 发送消息的标准程序示例。我正在使用 Spring Boot,也可以使用它的功能。
Spring Boot提供了一个RabbitTemplate,可以方便的发送消息:
@Component
public class Example {
private final RabbitTemplate rabbitTemplate;
public Example(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
@Override
public void run(String... args) throws Exception {
rabbitTemplate.convertAndSend(MessagingRabbitmqApplication.topicExchangeName, "foo.bar.baz", "Hello from RabbitMQ!");
}
请查看教程https://spring.io/guides/gs/messaging-rabbitmq/
另请阅读 Spring 引导文档:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-amqp
我是 RabbitMQ 的新手,正在尝试使用 JMS 向 RabbitMQ 发送消息。我有以下标准 JMS 生产者程序:
public void sendMessage() {
Context context = null;
ConnectionFactory factory = null;
Destination destination = null;
Connection connection = null;
Session session = null;
MessageProducer producer = null;
Properties initialProperties = new Properties();
initialProperties.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
initialProperties.put(InitialContext.PROVIDER_URL, "http-remoting://IP:PORT");
initialProperties.put(Context.SECURITY_PRINCIPAL, "mquser");
initialProperties.put(Context.SECURITY_CREDENTIALS, "Mquser@123");
try {
context = new InitialContext(initialProperties);
factory = (QueueConnectionFactory) context.lookup("jms/RemoteConnectionFactory");
System.out.println("Lookup Success");
destination = (Destination) context.lookup("jms/queue/TestQueue");
System.out.println("Queue lookup success");
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination);
String text = "8=FIX.4.49=7735=349=A56=B34=352=20200115-13:18:26.000 45=322222=D100208103222223=40558=Invalid FIX2ITFMsgType10=226";
TextMessage textMessage = session.createTextMessage();
textMessage.setText(text);
connection.start();
System.out.println("Going to send");
producer.send(textMessage);
System.out.println(this.getClass().getName() + "has sent a message : " + text);
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (context != null) {
try {
context.close();
} catch (NamingException ex) {
ex.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (JMSException ex) {
ex.printStackTrace();
}
}
}
}
我正在使用它向 JBoss EAP 7.2 中嵌入的 ActiveMQ Artemis 发送消息。正如我在 this link 中发现的那样,我将无法使用上述程序向 RabbitMQ 发送消息,但可以使用以下参数:
- 用户名
- 密码
- 虚拟主机
- 主持人
- 端口
- 目的地名称
- 交易所名称
- 队列名称
- 路由键
谁能给我一个向 RabbitMQ 发送消息的标准程序示例。我正在使用 Spring Boot,也可以使用它的功能。
Spring Boot提供了一个RabbitTemplate,可以方便的发送消息:
@Component
public class Example {
private final RabbitTemplate rabbitTemplate;
public Example(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
@Override
public void run(String... args) throws Exception {
rabbitTemplate.convertAndSend(MessagingRabbitmqApplication.topicExchangeName, "foo.bar.baz", "Hello from RabbitMQ!");
}
请查看教程https://spring.io/guides/gs/messaging-rabbitmq/
另请阅读 Spring 引导文档:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-amqp