List of usage examples for com.rabbitmq.client Channel basicConsume
String basicConsume(String queue, boolean autoAck, Consumer callback) throws IOException;
From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java
License:Apache License
/** * MQ ?? .//from ww w . jav a2 s .c om * <p> * exchange = fanout * ?? * * @param topic * @param consumer ? */ @Override protected void doSubscribe(String topic, Consumer<String> consumer) { Channel channel = rabbitAdapter.getConnection().createChannel(false); try { channel.exchangeDeclare(topic, BuiltinExchangeType.FANOUT, true); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, topic, ""); channel.basicQos(1); channel.basicConsume(queueName, false, getDefaultConsumer(channel, topic, topic, "", queueName, consumer)); } catch (IOException e) { logger.error("[MQ] Rabbit response error.", e); } }
From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java
License:Apache License
/** * MQ ?? .// w ww . j av a2 s . c o m * <p> * exchange = topic * ?? * * @param topic * @param routingKey Key * @param queueName ?? * @param consumer ? */ public void subscribeWithTopic(String topic, String routingKey, String queueName, Consumer<String> consumer) { Channel channel = rabbitAdapter.getConnection().createChannel(false); try { channel.queueDeclare(queueName, true, false, false, null); channel.exchangeDeclare(topic, BuiltinExchangeType.TOPIC, true); channel.queueBind(queueName, topic, routingKey); channel.basicQos(1); channel.basicConsume(queueName, false, getDefaultConsumer(channel, topic, topic, routingKey, queueName, consumer)); } catch (IOException e) { logger.error("[MQ] Rabbit subscribeWithTopic error.", e); } }
From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java
License:Apache License
@Override protected void doResponse(String address, Consumer<String> consumer) { Channel channel = rabbitAdapter.getConnection().createChannel(false); try {//from w w w. j a v a2s. c o m channel.queueDeclare(address, true, false, false, null); channel.basicQos(1); channel.basicConsume(address, false, getDefaultConsumer(channel, address, "", address, address, consumer)); } catch (IOException e) { logger.error("[MQ] Rabbit response error.", e); } }
From source file:mx.bigdata.utils.amqp.AMQPClientHelperImpl.java
License:Apache License
public QueueingConsumer createQueueingConsumer(Channel channel, String queue) throws Exception { QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queue, false, consumer); return consumer; }
From source file:mx.bigdata.utils.amqp.ReconnectingConsumer.java
License:Apache License
private boolean initConsumer() { Channel channel = null; try {// www . j a v a2s . c o m channel = amqp.declareChannel(factory, key); String queue = createQueue(amqp, channel, key); this.consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { ReconnectingConsumer.this.handleDelivery(consumerTag, envelope, properties, body); } @Override public void handleConsumeOk(String consumerTag) { ReconnectingConsumer.this.consumerTag = consumerTag; } @Override public void handleCancel(String consumerTag) throws IOException { logger.warn(tag + " handleCancel for consumer tag: " + consumerTag); try { this.getChannel().basicCancel(ReconnectingConsumer.this.consumerTag); } catch (Exception ignore) { } this.getChannel().getConnection().abort(5000); reconnect(); } @Override public void handleShutdownSignal(java.lang.String consumerTag, ShutdownSignalException sig) { try { getChannel().basicCancel(ReconnectingConsumer.this.consumerTag); } catch (Exception ignore) { ; } getChannel().getConnection().abort(5000); if (!sig.isInitiatedByApplication()) { logger.warn(tag + " ShutdownSignal for tag: " + tag + "\n\t consumer tag: " + consumerTag + "\n\t reason: " + sig.getReason() + "\n\t reference: " + sig.getReason() + "\n\t ", sig); reconnect(); } else { logger.debug(tag + " ShutdownSignal for tag: " + tag + "\n\t consumer tag: " + consumerTag + "\n\t reason: " + sig.getReason() + "\n\t reference: " + sig.getReason() + "\n\t ", sig); consumer = null; } } }; channel.basicConsume(queue, false, consumer); logger.info("Consumer " + tag + " initilized"); return true; } catch (Throwable e) { logger.error("Exception initializing consumer " + tag + ": ", e); if (channel != null) { channel.getConnection().abort(5000); } } return false; }
From source file:net.echinopsii.ariane.community.messaging.rabbitmq.ServiceFactory.java
License:Open Source License
/** * (internal usage)/*from w ww. j a v a 2 s . co m*/ * Create a new MomConsumer to consume message from RabbitMQ source and forward them to the request actor. * * @param source request source queue * @param channel RabbitMQ channel * @param requestActor request actor ref to treat the message * @param isMsgDebugOnTimeout debug on timeout if true * @return the new MomConsumer */ private static MomConsumer createConsumer(final String source, final Channel channel, final ActorRef requestActor, final boolean isMsgDebugOnTimeout) { return new MomConsumer() { private boolean isRunning = false; @Override public void run() { try { Map<String, Object> finalMessage; channel.queueDeclare(source, false, false, true, null); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(source, false, consumer); isRunning = true; while (isRunning) { try { QueueingConsumer.Delivery delivery = consumer.nextDelivery(10); if (delivery != null && isRunning) { finalMessage = translator.decode(new Message().setEnvelope(delivery.getEnvelope()) .setProperties(delivery.getProperties()).setBody(delivery.getBody())); if (((HashMap) finalMessage).containsKey(MomMsgTranslator.MSG_TRACE) && isMsgDebugOnTimeout) ((MomLogger) log).setMsgTraceLevel(true); ((MomLogger) log).traceMessage("MomConsumer(" + source + ").run", finalMessage); requestActor.tell(delivery, null); if (((HashMap) finalMessage).containsKey(MomMsgTranslator.MSG_TRACE) && isMsgDebugOnTimeout) ((MomLogger) log).setMsgTraceLevel(false); } } catch (InterruptedException e) { // no message } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (channel.getConnection() != null && channel.getConnection().isOpen()) { channel.queueDelete(source); //channel.close(); } } catch (IOException e) { e.printStackTrace(); } } } @Override public boolean isRunning() { return isRunning; } @Override public void start() { new Thread(this).start(); } @Override public void stop() { isRunning = false; try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } }; }
From source file:net.echinopsii.ariane.community.messaging.rabbitmq.ServiceFactory.java
License:Open Source License
/** * Create a new subscriber service./*from w w w . j a v a2 s . c om*/ * * @param baseSource the feed base source * @param selector the selector on the feed source (can be null) * @param feedWorker the feed message worker * @return the new subscriber service */ @Override public MomAkkaService subscriberService(final String baseSource, String selector, AppMsgWorker feedWorker) { MomAkkaService ret = null; ActorRef subsActor; MomConsumer consumer; final Connection connection = ((Client) super.getMomClient()).getConnection(); if (selector == null || selector.equals("")) selector = "#"; if (connection != null && connection.isOpen()) { subsActor = super.getMomClient().getActorSystem().actorOf(MsgSubsActor.props(feedWorker), baseSource + "." + ((selector.equals("#")) ? "all" : selector) + "_msgWorker"); final ActorRef runnableSubsActor = subsActor; final String select = selector; final Client cli = ((Client) super.getMomClient()); consumer = new MomConsumer() { private boolean isRunning = false; @Override public void run() { Channel channel = null; try { channel = connection.createChannel(); channel.exchangeDeclare(baseSource, "topic"); String queueName = cli.getClientID() + "_SUBS_2_" + baseSource + "." + select; channel.queueDeclare(queueName, false, true, false, null); channel.queueBind(queueName, baseSource, select); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); isRunning = true; while (isRunning) { try { QueueingConsumer.Delivery delivery = consumer.nextDelivery(10); if (delivery != null && isRunning) runnableSubsActor.tell(delivery, null); } catch (InterruptedException e) { e.printStackTrace(); } } if (channel.getConnection().isOpen()) channel.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (channel.getConnection().isOpen()) channel.close(); } catch (IOException e) { e.printStackTrace(); } } } @Override public boolean isRunning() { return isRunning; } @Override public void start() { new Thread(this).start(); } @Override public void stop() { isRunning = false; try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } }; consumer.start(); ret = new MomAkkaService().setMsgWorker(subsActor).setConsumer(consumer) .setClient(super.getMomClient()); super.getServices().add(ret); } return ret; }
From source file:nl.uva.sne.drip.drip.component_example.RPCServer.java
License:Apache License
private static void start() { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST);//from ww w . j a va2 s. c om factory.setPassword("guest"); factory.setUsername("guest"); factory.setPort(AMQP.PROTOCOL.PORT); try (Connection connection = factory.newConnection()) { Channel channel = connection.createChannel(); //We define the queue name channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null); //Set our own customized consummer Consumer c = new Consumer(channel); //Start listening for messages channel.basicConsume(RPC_QUEUE_NAME, false, c); //Block so we don't close the channel while (true) { try { Thread.sleep(100); } catch (InterruptedException _ignore) { } } } catch (IOException | TimeoutException ex) { Logger.getLogger(RPCServer.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:nl.uva.sne.drip.drip.provisioner.RPCServer.java
License:Apache License
private static void start() { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(PropertyValues.HOST); factory.setPassword("guest"); factory.setUsername("guest"); factory.setPort(AMQP.PROTOCOL.PORT); Logger.getLogger(RPCServer.class.getName()).log(Level.INFO, "Connected to: {0}", PropertyValues.HOST); try (Connection connection = factory.newConnection()) { Channel channel = connection.createChannel(); //We define the queue name channel.queueDeclare(PropertyValues.RPC_QUEUE_NAME, false, false, false, null); DefaultConsumer c;//from w w w .ja v a 2 s .com if (PropertyValues.RPC_QUEUE_NAME.endsWith("v0")) { c = new nl.uva.sne.drip.drip.provisioner.v0.Consumer(channel); } else { c = new nl.uva.sne.drip.drip.provisioner.v1.Consumer(channel, PropertyValues.HOST); } //Start listening for messages channel.basicConsume(PropertyValues.RPC_QUEUE_NAME, false, c); //Block so we don't close the channel while (true) { try { Thread.sleep(100); } catch (InterruptedException _ignore) { } } } catch (IOException | TimeoutException ex) { Logger.getLogger(RPCServer.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:normalizer_two.Normalizer_Two.java
public static void main(String[] args) throws IOException, InterruptedException { ConnectionCreator creator = ConnectionCreator.getInstance(); com.rabbitmq.client.Channel channelIn = creator.createChannel(); com.rabbitmq.client.Channel channelOut = creator.createChannel(); channelIn.queueDeclare(IN_QUEUE, false, false, false, null); channelOut.queueDeclare(OUT_QUEUE, false, false, false, null); QueueingConsumer consumer = new QueueingConsumer(channelIn); channelIn.basicConsume(IN_QUEUE, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); //channelIn.basicAck(delivery.getEnvelope().getDeliveryTag(), false); System.out.println(new String(delivery.getBody())); System.out.println("CorrelationID" + delivery.getProperties().getCorrelationId()); String message = translateMessage(delivery); BasicProperties prop = new BasicProperties().builder() .correlationId(delivery.getProperties().getCorrelationId()).build(); channelOut.basicPublish("", OUT_QUEUE, prop, message.getBytes()); }/*w w w . ja v a2s . c o m*/ }