Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to manually connect to RabbitMq and send messages to producers

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report