简体   繁体   English

使用 JMS 向 RabbitMQ 发送消息

[英]Sending message to RabbitMQ using JMS

I am new to RabbitMQ and trying to send a message to RabbitMQ using JMS.我是 RabbitMQ 的新手,并尝试使用 JMS 向 RabbitMQ 发送消息。 I have the below standard JMS producer program:我有以下标准的 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();
            }
        }
    }
}

Which I am using to send message to ActiveMQ Artemis embedded in JBoss EAP 7.2.我使用它向嵌入在 JBoss EAP 7.2 中的 ActiveMQ Artemis 发送消息。 As I have found in this link I will not be able to use the above program to send message to RabbitMQ, but will of the below parameters:正如我在此链接中发现的那样,我将无法使用上述程序向 RabbitMQ 发送消息,但可以使用以下参数:

  1. Username用户名
  2. Password密码
  3. VirtualHost虚拟主机
  4. Host主持人
  5. Port港口
  6. DestinationName目的地名称
  7. ExchangeName交易所名称
  8. QueueName队列名称
  9. RoutingKey路由密钥

Can anyone give me an example of a standard program for sending message to RabbitMQ.谁能给我一个向RabbitMQ发送消息的标准程序的例子。 I am using Spring Boot and can also use its features.我正在使用 Spring Boot,也可以使用它的功能。

Spring Boot provides a RabbitTemplate that makes it easy to send messages: 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!");
  }

Please checkout the tutorial https://spring.io/guides/gs/messaging-rabbitmq/请查看教程https://spring.io/guides/gs/messaging-rabbitmq/

Please also read the Spring Boot documentation: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-amqp另请阅读 Spring Boot 文档: https : //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-amqp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM