柚子快報(bào)激活碼778899分享:RabbitMQ
柚子快報(bào)激活碼778899分享:RabbitMQ
rabbitmq官網(wǎng):https://www.rabbitmq.com/ rabbitmq是erlang開(kāi)發(fā)的,需要安裝erlang環(huán)境 Erlang是一種功能編程語(yǔ)言,也具有運(yùn)行時(shí)環(huán)境。 Erlang官網(wǎng):https://www.erlang.org/
獲取連接
public class RabbitMQConnection {
public static Connection getConnection() throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory();
// 設(shè)置連接虛擬機(jī)
connectionFactory.setVirtualHost("/meiteVirtualHosts");
// 設(shè)置賬號(hào)密碼
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
// 設(shè)置ip和端口
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
return connectionFactory.newConnection();
}
}
生產(chǎn)者
public class Producer {
public static final String QUEUE_NAME = "queue-name";
public static void main(String[] args) throws IOException, TimeoutException {
// 創(chuàng)建連接
Connection connection = RabbitMQConnection.getConnection();
// 創(chuàng)建通道
Channel channel = connection.createChannel();
String msg = "消息-" + new Date();
// 發(fā)布消息到隊(duì)列
channel.basicPublish("",QUEUE_NAME,null,msg.getBytes());
// 關(guān)閉通道和連接
channel.close();
connection.close();
}
}
消費(fèi)者
public class Comsumer {
public static final String QUEUE_NAME = "queue-name";
public static void main(String[] args) throws IOException, TimeoutException {
// 創(chuàng)建連接
Connection connection = RabbitMQConnection.getConnection();
// 創(chuàng)建通道
Channel channel = connection.createChannel();
// 消費(fèi)消息
DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("消費(fèi)者:" + new String(body, StandardCharsets.UTF_8));
}
};
// 監(jiān)聽(tīng)隊(duì)列 autoAck:true自動(dòng)簽收,false手動(dòng)簽收
channel.basicConsume(QUEUE_NAME,true,defaultConsumer);
// 長(zhǎng)連接不必關(guān)閉通道
//channel.close();
//connection.close();
}
}
柚子快報(bào)激活碼778899分享:RabbitMQ
推薦文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。