2010-11-11 13 views

Répondre

2

Lors de l'envoi d'un message via le client java habituellement il est publich à un canal comme

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

ici u peut définir les propriétés de message

u van obtenir le msg en utilisant une livraison agent vous devez d'abord lier la file d'attente comme ceci

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

Utilisez ensuite delivey pour obtenir msg

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

      } catch (InterruptedException ie) { 
       continue; 
      } 
0

Voici comment il peut être fait:

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