rabbitmq的通过api实现的demo

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);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值