Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to manually connect to RabbitMq and send messages to producers

Shulou Source: shulou.com Published: 2022-06-02 07:32:34 10月01日 Update

This article mainly explains "how to manually connect RabbitMq and producers to send messages", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "RabbitMq how to manually connect and producers send messages" bar!

Import java.io.IOException

Import java.io.UnsupportedEncodingException

Import java.util.concurrent.TimeoutException

Import javax.annotation.PostConstruct

Import org.slf4j.Logger

Import org.slf4j.LoggerFactory

Import org.springframework.beans.factory.annotation.Value

Import org.springframework.stereotype.Component

Import com.rabbitmq.client.Channel

Import com.rabbitmq.client.Connection

Import com.rabbitmq.client.ConnectionFactory

@ Component

Public class RabbitSender {

Private static final Logger logger = LoggerFactory.getLogger (RabbitSender.class)

Private Channel channel = null

@ Value ("${btc.mes.spring.rabbitmq.host:10.86.210.15}")

Private String addr

@ Value ("${btc.mes.spring.rabbitmq.port:5672}")

Private int port

@ Value ("${btc.mes.spring.rabbitmq.username:cmes_weirui}")

Private String username

@ Value ("${btc.mes.spring.rabbitmq.password:cmes_weirui@14&3}")

Private String password

@ Value ("${btc.mes.spring.rabbitmq.mq:batterytraceability}")

Private String mq;// queue name

@ PostConstruct

Public void init () {

ConnectionFactory connectionFactory = new ConnectionFactory ()

ConnectionFactory.setHost (addr)

ConnectionFactory.setPort (port)

ConnectionFactory.setUsername (username); / / user name

ConnectionFactory.setPassword (password); / / password

Try {

Connection con = connectionFactory.newConnection ()

Channel = con.createChannel ()

} catch (IOException e) {

E.printStackTrace ()

} catch (TimeoutException e) {

E.printStackTrace ()

}

}

/ / to send messages, there is no need to implement any interfaces for external calls.

Public void send (String msg) {

Logger.info ("start sending message:" + msg)

Try {

Channel.basicPublish ("", mq,null,msg.getBytes ("UTF-8"))

} catch (UnsupportedEncodingException e) {

E.printStackTrace ()

} catch (IOException e) {

E.printStackTrace ()

}

Logger.info ("end sending message:" + msg)

}

}

Thank you for your reading, the above is the content of "how to manually connect RabbitMq and producers to send messages". After the study of this article, I believe you have a deeper understanding of how to manually connect RabbitMq and producers to send messages, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Message manual producer production learning content name password that is ideas situations interfaces articles more users user names knowledge knowledge points articles follow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi NVidia Shulou Tech Info Shulou Information