Created
June 25, 2012 17:50
-
-
Save anonymous/2990146 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Map; | |
import java.io.IOException; | |
import com.rabbitmq.client.AMQP; | |
import com.rabbitmq.client.ConnectionFactory; | |
import com.rabbitmq.client.Connection; | |
import com.rabbitmq.client.Channel; | |
import com.rabbitmq.client.DefaultConsumer; | |
import com.rabbitmq.client.Envelope; | |
public class Recv { | |
private final static String QUEUE_NAME = "xxx"; | |
private final static String HOST_NAME = "x.x.x.x"; | |
public static void main(String[] args) throws java.io.IOException, | |
java.lang.InterruptedException { | |
ConnectionFactory factory = new ConnectionFactory(); | |
Map<String, Object> clientProperties = new HashMap<String, Object>(); | |
clientProperties.put("consumer_cancel_notify", Boolean.valueOf(true)); | |
factory.setHost(HOST_NAME); | |
factory.setClientProperties(clientProperties); | |
Connection connection = factory.newConnection(); | |
final Channel channel = connection.createChannel(); | |
channel.basicQos(1); | |
Map<String, Object> queueArgs = new HashMap<String, Object>(); | |
queueArgs.put("x-ha-policy", "all"); | |
channel.queueDeclare(QUEUE_NAME, true, false, false, queueArgs); | |
System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); | |
try { | |
channel.basicConsume(QUEUE_NAME, false, | |
new DefaultConsumer(channel) { | |
@Override | |
public void handleDelivery(String consumerTag, | |
Envelope envelope, | |
AMQP.BasicProperties properties, byte[] body) | |
throws IOException { | |
long deliveryTag = envelope.getDeliveryTag(); | |
String message = new String(body); | |
System.out.println(" [x] Received '" + message | |
+ "'"); | |
channel.basicAck(deliveryTag, false); | |
} | |
@Override | |
public void handleCancel(String consumerTag) throws IOException { | |
System.out.println("Cancelled"); | |
} | |
}); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment