2010-11-11 182 views

回答

2

雖然發送通過Java客戶端的消息通常是publich像

CHANNEL.basicPublish(EXCHANGE_NAME, QUEUE_ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN, "message".getBytes) 

這裏u能設置消息屬性的信道

ü麪包車通過使用遞送得到MSG代理 你必須先像這樣綁定隊列

Channel channel = conn.createChannel(); 
     String exchangeName = "myExchange"; 
     String queueName = "myQueue"; 
     String routingKey = "testRoute"; 
     boolean durable = true; 
     channel.exchangeDeclare(exchangeName, "direct", durable); 
     channel.queueDeclare(queueName, durable,false,false,durable, null); 
channel.queueBind(queueName, exchangeName, routingKey); 
     boolean noAck = false; 
     QueueingConsumer consumer = new QueueingConsumer(channel); 
     channel.basicConsume(queueName, noAck, consumer); 

然後使用delivey獲得味精

QueueingConsumer.Delivery delivery; 
      try { 
       delivery = consumer.nextDelivery(); 

      } catch (InterruptedException ie) { 
       continue; 
      } 
0

這裏是如何可以做到:

int PERSISTENCE_MESSAGE = 2; // Persist message 
String TEXT_MESSAGE = "text/plain"; 
String queueName = "QUE-1"; 

Channel channel = this.connection.createChannel(); 
channel.queueDeclare(queueName, true, false, false, null); 

// Build message properties 
Map messageProps = new HashMap(); 
//messageProps.put("TIME_MSG_RECEIVED", time); 
messageProps.put("SOURCE_SYS", "SRC1"); 
messageProps.put("DESTINATION_SYS", "DST1"); 

// Set message properties 
AMQP.BasicProperties.Builder basicProperties = new AMQP.BasicProperties.Builder(); 
basicProperties.contentType(TEXT_MESSAGE).deliveryMode(PERSISTENCE_MESSAGE) 
.priority(1).headers(messageProps); 

channel.basicPublish("", queueName, basicProperties.build(), message.getBytes()); 
System.out.println(" Sent message to RabbitMQ: '" + message + "'"); 
channel.close();