码迷,mamicode.com
首页 > Windows程序 > 详细

rabbitMQ 常用api翻译

时间:2017-12-29 14:59:31      阅读:379      评论:0      收藏:0      [点我收藏+]

标签:clu   when   ali   struct   ace   sage   ued   发布   lis   

1.首先是发布消息者的代码

 

public class MQPublisher {
    public static void main(String[] args) {
        try{
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("123.199.200.54");//129.199.200.54
            factory.setUsername("omsuser");
            factory.setPassword("9aVXR2LLk2xskcDq7ziccWKP");
            factory.setPort(5672);
            Connection connection = factory.newConnection();

            Channel channel = connection.createChannel();
            channel.queueDeclare("ManufactureBill", true, false, false, null);
            String bill = getManufactureBill();
            MQMessage mqmsg=new MQMessage();
            JSONObject jso=JSON.parseObject(bill);//json字符串转换成jsonobject对象
            mqmsg.setObject(jso);
            mqmsg.setType(0);
            String MQjson = JSON.toJSONString(mqmsg,SerializerFeature.WriteMapNullValue);
            //channel.basicPublish("", "ManufactureBill", null, bill.getBytes());
           // channel.queueDelete("ManufactureBill");
            channel.basicPublish("", "ManufactureBill", null, MQjson.getBytes());
           
            channel.close();
            connection.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

 定义通道

AMQP.Queue.DeclareOk queueDeclare(String queue,
                                  boolean durable,
                                  boolean exclusive,
                                  boolean autoDelete,
                                  Map<String,Object> arguments)
                           throws IOException
Declare a queue
Parameters:
queue - the name of the queue 通道的名称,消费者要接收信息,名称要与此相同,同一个连接下面会可能有多个通道
durable - true if we are declaring a durable queue (the queue will survive a server restart)  定义一个持久的队列,一般设置为true,当服务器挂掉之后,队列依然存在,
exclusive - true if we are declaring an exclusive queue (restricted to this connection)   定义一个独有队列(不允许别人连接)一般设置为false,不然怎么接受消息
autoDelete - true if we are declaring an autodelete queue (server will delete it when no longer in use)  当服务器不在用到此队列的时候,是否自动删除。一般设置为false,和durable一致
arguments - other properties (construction arguments) for the queue  这个不知道
Returns:
a declaration-confirm method to indicate the queue was successfully declared
Throws:
IOException - if an error is encountered
See Also:
AMQP.Queue.Declare, AMQP.Queue.DeclareOk

 

rabbitMQ 常用api翻译

标签:clu   when   ali   struct   ace   sage   ued   发布   lis   

原文地址:https://www.cnblogs.com/longsanshi/p/8143830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!