RabbitMQ第一个案例:hello world

来源:互联网 发布:华通云数据与马云关系 编辑:程序博客网 时间:2024/06/06 05:44

前提:把rabbitMQ安装完成

Producer:

package yy.rabbitstudy;import java.io.IOException;import java.util.concurrent.TimeoutException;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;public class Send {    private final static String QUEUE_NAME = "hello";    public static void main(String[] args) {        ConnectionFactory factory = new ConnectionFactory();        factory.setHost("localhost");        Connection connection = null;        Channel channel = null;        try {            //获取连接            connection = factory.newConnection();            //创建通道            channel = connection.createChannel();            //打开队列位置            channel.queueDeclare(QUEUE_NAME, false, false, false, null);            String message = "Hello World RabbitMQ";            //把数据发布到队列            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());            System.out.println("[x] Sent" + message + "");        } catch (IOException e) {            e.printStackTrace();        } catch (TimeoutException e) {            e.printStackTrace();        } finally {            if (channel != null) {                try {                    channel.close();                } catch (IOException e) {                    e.printStackTrace();                } catch (TimeoutException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                            }            if (connection != null) {                try {                    connection.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }            }}



Consumer:


package yy.rabbitstudy;import java.io.IOException;import java.util.concurrent.TimeoutException;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;public class Recv {    private final static String QUEUE_NAME="hello";    public static void main(String[] args) throws IOException, TimeoutException {                ConnectionFactory factory=new ConnectionFactory();        factory.setHost("localhost");        Connection connection=factory.newConnection();        Channel channel=connection.createChannel();        //打开通道        channel.queueDeclare(QUEUE_NAME,false,false,false,null);        System.out.println("[*] Waiting for messages. to exit press CTRL+C");               //把通道交给消费者        Consumer consumer = new DefaultConsumer(channel) {            @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(" [x] Received '" + message + "'");            }          };          channel.basicConsume(QUEUE_NAME, true, consumer);    }}


0 0
原创粉丝点击