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