java-rabbitmq-实例pull模式拉取消息

java-rabbitmq-实例pull模式拉取消息


描述:
手动拉取指定队列的消息。


运行:
D7_PullSend.main();
D7_PullRecv.main();

package com.example.tutorials;


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


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


/**
 * 使用 channel.basicGet() 方法,拉取指定队列中的内容
 * @create 2017-09-05
 * amqp-client 4.2.0
 **/
public class D7_PullSend {
    private final static String QUEUE_NAME = "pull_queue";


    /**
     * 生产者,
     * @param argv
     * @throws Exception
     */
    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        //设置登录账号
        factory.setHost(ServerInfo.host);
        factory.setPort(ServerInfo.port);
        factory.setUsername(ServerInfo.uName);
        factory.setPassword(ServerInfo.uPwd);
        //链接服务器
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();


        Map<String,Object> args = new HashMap<>();
//        args.put("x-message-ttl",15*1000);//消息过期时间为 15 秒
        //args.put("x-expires",1*60*1000); //队列过期时间为 1 分钟
        //定义一个队列
        boolean duiable=false;//持久化
        boolean exclusive = false;//排他队列
        boolean autoDelete=false;//没有consumer时,队列是否自动删除
        channel.queueDeclare(QUEUE_NAME, duiable, exclusive, autoDelete, args);


        //发送消息
        System.out.println("输入要发送的消息,退出输入 x ");
        String message ;
        //
        AMQP.BasicProperties prop = new AMQP.BasicProperties
                .Builder()
                .expiration("15000") //消息过期时间为 15 秒
                .build();
        do{
            Scanner scanner = new Scanner(System.in);
            message = scanner.next();
            channel.basicPublish(""
                    , QUEUE_NAME
                    , prop
                    , message.getBytes("UTF-8"));
            System.out.println(" [x] Sent '" + message + "'");
        }while(!"x".equals(message));
        //关闭链接
        channel.close();
        connection.close();
    }
}





package com.example.tutorials;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.GetResponse;


import java.util.Scanner;


/**
 * 使用 channel.basicGet() 方法,拉取指定队列中的内容
 * @create 2017-09-05
 * amqp-client 4.2.0
 **/
public class D7_PullRecv {
    private final static String QUEUE_NAME = "pull_queue";


    /**
     * 消费者, "Hello World!"
     * @param argv
     * @throws Exception
     */
    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        //设置登录账号
        factory.setHost(ServerInfo.host);
        factory.setPort(ServerInfo.port);
        factory.setUsername(ServerInfo.uName);
        factory.setPassword(ServerInfo.uPwd);
        //链接服务器
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        //定义一个队列
        boolean duiable=false;//持久化
        boolean exclusive = false;//排他队列
        boolean autoDelete=false;//没有consumer时,队列是否自动删除
        channel.queueDeclare(QUEUE_NAME, duiable, exclusive, autoDelete, null);


        //接收消息
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
        boolean autoAck = true; //自动应答


        System.out.println("输入回车获取消息,退出输入 x ");
        String message ="";
        GetResponse resp ;
        do{
            Scanner scanner = new Scanner(System.in);
            scanner.nextLine();
            resp = channel.basicGet(QUEUE_NAME,autoAck);
            if(resp==null){
                System.out.println(QUEUE_NAME+" 队列无消息");
                continue;
            }
            message = new String(resp.getBody(), "UTF-8");
            System.out.println(String.format(" [x] Recv Count %s , msg = %s;"
                ,resp.getMessageCount()
                ,message));
        }while(!"x".equals(message));
    }
}


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值