rabbitmq的通过api实现的demo

来源:互联网 发布:编程之魂 译者 编辑:程序博客网 时间:2024/06/06 03:43

1、maven依赖

<dependency><groupId>com.rabbitmq</groupId><artifactId>amqp-client</artifactId><version>4.2.0</version></dependency>

2、发送端代码

import java.io.IOException;import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;/** * * @ClassName: Send* @Description: TODO(The sender will connect to RabbitMQ, send a single message, then exit.)* @date 2017年11月29日 下午5:20:19**/public class Sender {    /*     * 定义一个队列“hello”     */    private final static String QUEUE_NAME = "TestMQ";     public static void main(String[] argv) throws IOException, TimeoutException{        //创建一个连接        ConnectionFactory factory = new ConnectionFactory();                //连接本地,如果需要指定到服务,需在这里指定IP        factory.setHost("183.63.252.158");        factory.setUsername("admin");        factory.setPassword("123456");        factory.setPort(5672);        Connection connection = factory.newConnection();                //创建一个通道        Channel channel = connection.createChannel();                //申明通道发送消息的队列,把消息发送至消息队列‘hello’        channel.queueDeclare(QUEUE_NAME, false, false, false, null);        String message = "Hello 你好!";                //Declaring a queue is idempotent - 如果队列不存在则会创建一个队列         //消息内容为byte array, so可以自己随意编码        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());        System.out.println(" 已经发出的消息 :'" + message + "'");         //消息发送完成后,关闭通道和连接        channel.close();        connection.close();    }}

3、接收端代码

import java.io.IOException; import com.rabbitmq.client.AMQP;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;import com.rabbitmq.client.Consumer;import com.rabbitmq.client.DefaultConsumer;import com.rabbitmq.client.Envelope; /** * * @ClassName: Recv* @Description: TODO(接收消息类)* @author czq* @date 2017年11月29日 下午5:33:27**/public class Receiver{     private final static String QUEUE_NAME = "TestMQ";     public static void main(String[] args) throws Exception{            //创建一个连接        ConnectionFactory factory = new ConnectionFactory();                //连接本地,如果需要指定到服务,需在这里指定IP        factory.setHost("183.63.252.158");        factory.setUsername("admin");        factory.setPassword("123456");        factory.setPort(5672);        Connection connection = factory.newConnection();                //创建一个通道        Channel channel = connection.createChannel();                //申明接收消息的队列,与发送消息队列"hello"对应        channel.queueDeclare(QUEUE_NAME, false, false, false, null);        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");        //The extra DefaultConsumer is a class implementing the Consumer interface         //we'll use to buffer the messages pushed to us by the server.        Consumer consumer = new DefaultConsumer(channel){                    //重写DefaultConsumer中handleDelivery方法,在方法中获取消息            @Override            public void handleDelivery(String consumerTag, Envelope envelope,                     AMQP.BasicProperties properties, byte[] body) throws IOException{                String message = new String(body, "UTF-8");                System.out.println(" 收到了消息: '" + message + "'");            }        };        channel.basicConsume(QUEUE_NAME, true,consumer);    }}


原创粉丝点击