java 使用RabbitMQ 队列

RabbitMQ 的简单使用

1.引入jar包

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

 2.信息发送者(生产者)

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 *  生产者
 */
public class ComProducer {
    
    public static void main(String[] args) throws IOException, TimeoutException {

        String QUEUE_NAME = "aque";
        String EXCHANGE_NAME = "aexc";
        String ROUTING_KEY= "aaa";
        
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("127.0.0.1");
        cf.setPort(5672);
        cf.setUsername("guest");
        cf.setPassword("guest");
        
        //建立连接
        Connection conn = cf.newConnection();
        //创建消息通道
        Channel channel = conn.createChannel();


        //消息内容
        String msg = "hello world!!!! 你好啊~";
        
        //创建 队列
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        //创建路由
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        //绑定
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
        
        //发送消息
        channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, null, msg.getBytes());
         
        channel.close();
        conn.close();

    }
}
3.信息接受者(消费者)
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.ConsumerCancelledException;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.ShutdownSignalException;

/**
 * 消费者
 */
public class ComConsumer {

    public static void main(String[] args) throws IOException, TimeoutException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {
        

        String QUEUE_NAME = "aque";
        String EXCHANGE_NAME = "aexc";
        
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("127.0.0.1");
        cf.setPort(5672);
        cf.setUsername("guest");
        cf.setPassword("guest");
        
        //建立连接
        Connection conn = cf.newConnection();
        //创建消息通道
        Channel channel = conn.createChannel();
        
        //创建消费者,并接受消息
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
                    throws IOException {
                String msg = new String(body, "UTF-8");
                System.out.println("接收消息:"+msg);
               
            }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);
        

    }
    
}




猜你喜欢

转载自blog.csdn.net/liyongbing1122/article/details/81060391