List of usage examples for com.rabbitmq.client Channel basicConsume
String basicConsume(String queue, Consumer callback) throws IOException;
From source file:wunderrabbit.RabbitConnection.java
License:Apache License
@Override public Listener listen(final Endpoint endpoint, final MessageHandler handler, Map<ListenOption, Object> options) throws Exception { Channel channel = this.connection.createChannel(); Consumer consumer = new DefaultConsumer(channel) { public void handleDelivery(String tag, Envelope envelope, BasicProperties properties, byte[] body) { RabbitMessage message = new RabbitMessage(body, properties, endpoint); try { handler.onMessage(message); } catch (Exception e) { e.printStackTrace();//www. ja v a2s . c om } } }; //TODO: concurrency declareQueue(channel, endpoint); String tag = channel.basicConsume((String) endpoint.implementation(), consumer); return new RabbitListener(tag, channel); }
From source file:ylj.demo.mq.amqp.rabbitmq_client.DemoConsumer.java
License:Apache License
public static void doConsume() throws Exception { ExecutorService consumeES = Executors.newFixedThreadPool(2); ConnectionFactory factory = new ConnectionFactory(); factory.setUri("amqp://guest:guest@newsrec10.photo.163.org:5672"); // factory.setUri("amqp://guest:guest@/?brokerlist='tcp://newsrec10.photo.163.org:5672'"); factory.setAutomaticRecoveryEnabled(true); factory.setNetworkRecoveryInterval(10000); factory.setExceptionHandler(new MyExceptionHandler()); AutorecoveringConnection conn = (AutorecoveringConnection) factory.newConnection(consumeES); conn.addRecoveryListener(new MyRecoveryListener()); // ?queue??/*w w w .j a v a2s. c om*/ Channel channel = conn.createChannel(); // channel.close(); // channel.queueDeclare("recsys.news.userfeed.usermodel", false, false, false, null); // channel.basicConsume("recsys.news.userfeed.usermodel", true, null, new MyConsumer()); channel.basicConsume("recsys.news.userfeed.usermodel", new MyConsumer()); // conn.close(); /* * work??work???? channel.basicConsume("hello",false,consumer); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); * */ }