RabbitMQ 消息发送和消息获取 之 rabbitMQ消息生产者和消费者
2014-09-26 00:00
447 查看
//rabbitMQ消息生产者 、发送消息 public static void shenc() throws IOException{ //创建链接工程 ConnectionFactory factory2 = new ConnectionFactory(); factory2.setHost("xx.xx.xx.xx"); factory2.setUsername("name"); factory2.setPassword("pass"); factory2.setVirtualHost("virtualhost 日志文件名称");//本地无需这项set //创建链接 Connection connection = factory2.newConnection(); //创建消息通道 Channel channel = connection.createChannel(); String message = "Hello World"; String Queue = "Queue"; //生命一个消息队列 channel.queueDeclare(Queue, true, false, false, null); //发布消息,第一个参数表示路由(Exchange名称),未""则表示使用默认消息路由 channel.basicPublish("", Queue, MessageProperties.PERSISTENT_TEXT_PLAIN,message .getBytes()); log.info("传送信息完毕"); System.out.println(" [x] Sent '"+message+"'"); //关闭消息通道和链接 channel.close(); connection.close(); }
//消费者、获取MQ消息 public static void xiaofei() throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException{ //创建链接工厂 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("xx.xx.xx.xx"); factory.setUsername("name"); factory.setPassword("pass"); factory.setVirtualHost("virtualhost 日志文件名称");//本地无需这项set //创建链接 Connection connection = factory.newConnection(); String Queue = "Queue"; //创建消息信道 Channel channel = connection.createChannel(); //生命消息队列 channel.queueDeclare(Queue,true,false,false,null); //消费者用于获取消息信道绑定的消息队列中的信息 QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(Queue, true,consumer); while(true){ //循环获取消息队列中的信息 QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println("[x] Received '"+message+"'"); } }
相关文章推荐
- RabbitMQ消息通信,生产者发送消息给指定的消费者的消息队列
- RabbitMQ-理解消息通信-消费者和生产者
- RabbitMQ消息通信,一个生产者和多个消费者
- Disruptor多个消费者不重复处理生产者发送的消息的demo
- RabbitMQ消息队列之二:消费者和生产者
- 消息队列_RabbitMQ-0002.深入MQ生产者/信道/交换机/队列/消费者?
- Disruptor多个消费者不重复处理生产者发送过来的消息
- RabbitMQ消息队列生产者和消费者
- kafka(java客户端)消费者取不到消息,生产者消息也没发送成功
- RabbitMQ消息队列之二:消费者和生产者 Demo
- 互斥锁和条件变量(2)——生产者和消费者(发送消息,循环队列执行)
- spring JMS、activemq中消费者收不到生产者发送的消息的原因解析
- RabbitMQ消息通信,一个生产者和多个消费者,广播式消息通信
- 消息队列RabbitMQ和ActiveMQ的生产者流量控制
- 消息队列RabbitMQ和ActiveMQ的生产者流量控制
- rabbitmq一---一对一发送及接收消息
- XMPP学习记录四:关于接受,发送消息和获取好友上下线的状态
- activemq消息生产者与消息消费者简单例子
- 在java程序中如何能获取到另一个程序的windows句柄,又如何使用这个句柄向这个窗口发送消息
- java实现rabbitmq消息的发送接受