您好,登錄后才能下訂單哦!
這篇文章主要講解了“RabbitMq如何手動(dòng)連接及生產(chǎn)者發(fā)送消息”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“RabbitMq如何手動(dòng)連接及生產(chǎn)者發(fā)送消息”吧!
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;//隊(duì)列名稱
@PostConstruct
public void init() {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost(addr);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);//用戶名
connectionFactory.setPassword(password);//密碼
try {
Connection con = connectionFactory.newConnection();
channel = con.createChannel();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
//發(fā)送消息,不需要實(shí)現(xiàn)任何接口,供外部調(diào)用。
public void send(String msg){
logger.info("開始發(fā)送消息 : " + msg);
try {
channel.basicPublish("",mq,null,msg.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
logger.info("結(jié)束發(fā)送消息 : " + msg);
}
}
感謝各位的閱讀,以上就是“RabbitMq如何手動(dòng)連接及生產(chǎn)者發(fā)送消息”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)RabbitMq如何手動(dòng)連接及生產(chǎn)者發(fā)送消息這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。