public String recvFromMessagequeue() { RabbitmqClientConf client = new RabbitmqClientConf(); Channel channel = client.getChannel(); try { channel.queueDeclare(QUEUE_NAME, false, false, false, null); } catch (IOException e) { e.printStackTrace(); } System.out.println(" [*] Waiting for messages, To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); try { channel.basicConsume(QUEUE_NAME, true, consumer); } catch (IOException e) { e.printStackTrace(); } System.out.println("ready to receive"); QueueingConsumer.Delivery delivery = null; try { delivery = consumer.nextDelivery(); } catch (InterruptedException e) { e.printStackTrace(); } String message = new String(delivery.getBody()); System.out.println("[x] Received'" + message + "'"); return message; }
public static void main(String[] args) throws IOException, InterruptedException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); boolean durable = true; channel.queueDeclare(QUEUE_NAME, durable, false, false, null); System.out.println("[*] waiting for messages.To exit press ctrl+z"); // 公平分发 // 告知RabbitMQ不要同时给一个工作者超过一个任务 // 1个工作者处理完成,发送确认之前不要给它分发一个新的消息 int prefetchcount = 1; channel.basicQos(prefetchcount); QueueingConsumer consumer = new QueueingConsumer(channel); boolean autoAck = false; channel.basicConsume(QUEUE_NAME, false, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); // 模拟工作 doWork(message); System.out.println(" [x] Done"); // 发送确认消息 channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } }
public static void main(String[] argv) throws java.io.IOException, java.lang.InterruptedException, TimeoutException { // 创建连接和频道 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // 声明direct类型转发器 channel.exchangeDeclare(EXCHANGE_NAME, "direct"); String queueName = channel.queueDeclare().getQueue(); String severity = getSeverity(); // 指定binding_key channel.queueBind(queueName, EXCHANGE_NAME, severity); System.out.println(" [*] Waiting for " + severity + " logs. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); } }
public void receiveMessage(String qName) { try { CachingConnectionFactory factory = new CachingConnectionFactory(); factory.setAddresses("10.30.135.103:5672,10.30.135.101:5672"); org.springframework.amqp.rabbit.connection.Connection connection = factory.createConnection(); Channel channel = connection.createChannel(false); // channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); // ring queueName = channel.queueDeclare().getQueue(); // channel.queueBind(queueName, EXCHANGE_NAME, ""); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(qName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); } } catch (Exception e) { e.printStackTrace(); } }
@Test public void testBasicConnectivity() { String sentMsg = "sent"; String recvdMsg = "received"; try { channel.queueDeclare(basicTestQueue, false, false, false, null); sentMsg = "testBasicConnectivity"; channel.basicPublish("", basicTestQueue, null, sentMsg.getBytes()); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(basicTestQueue, true, consumer); QueueingConsumer.Delivery delivery = consumer.nextDelivery(1500); if (delivery != null) { recvdMsg = new String(delivery.getBody()); } } catch (Exception ex) { fail("Connectivity error to MQ while running Test" + ex); } finally { try { channel.close(); connection.close(); } catch (Exception ex) { fail("Connectivity error to MQ while running Test" + ex); } } assertEquals(sentMsg, recvdMsg); }
public static void main(String[] arg) throws IOException, InterruptedException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct"); String queueName = channel.queueDeclare().getQueue(); if (arg.length < 1) { System.err.println("Usage: ReceiveLogsDirect [info] [warning] [error]"); System.exit(1); } for (String severity : arg) { channel.queueBind(queueName, EXCHANGE_NAME, severity); } System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); String routingKey = delivery.getEnvelope().getRoutingKey(); System.out.println(" [x] Received '" + routingKey + "':'" + message + "'"); } }
public static void main(String[] args) throws Exception { // 获取到连接以及mq通道 Connection connection = ConnectionUtil.getConnection(); Channel channel = connection.createChannel(); // 声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 绑定队列到交换机 channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "#.#"); // 同一时刻服务器只会发一条消息给消费者 channel.basicQos(1); // 定义队列的消费者 QueueingConsumer consumer = new QueueingConsumer(channel); // 监听队列,手动返回完成 channel.basicConsume(QUEUE_NAME, false, consumer); // 获取消息 while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); Thread.sleep(10); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } }
/** Test that an AMQP client can connect to and use the broker. */ @Test(groups = {"Integration", "WIP"}) public void testClientConnection() throws Exception { rabbit = app.createAndManageChild(EntitySpec.create(RabbitBroker.class)); rabbit.start(ImmutableList.of(testLocation)); EntityTestUtils.assertAttributeEqualsEventually(rabbit, Startable.SERVICE_UP, true); byte[] content = "MessageBody".getBytes(Charsets.UTF_8); String queue = "queueName"; Channel producer = null; Channel consumer = null; try { producer = getAmqpChannel(rabbit); consumer = getAmqpChannel(rabbit); producer.queueDeclare(queue, true, false, false, ImmutableMap.<String, Object>of()); producer.queueBind(queue, AmqpExchange.DIRECT, queue); producer.basicPublish(AmqpExchange.DIRECT, queue, null, content); QueueingConsumer queueConsumer = new QueueingConsumer(consumer); consumer.basicConsume(queue, true, queueConsumer); QueueingConsumer.Delivery delivery = queueConsumer.nextDelivery(60 * 1000l); // one minute timeout assertEquals(delivery.getBody(), content); } finally { closeSafely(producer, 10 * 1000); closeSafely(consumer, 10 * 1000); } }
public static void main(String[] args) throws IOException, InterruptedException { Random random = new Random(); int i = random.nextInt(10); String clientName = "client-" + i; Channel channel = RabbitUtil.getChannel(); // 申明交换器 channel.exchangeDeclare(C.FANOUT_EXCHANGE, "fanout"); // 匿名队列 String queueName = channel.queueDeclare().getQueue(); System.err.println("queueName:" + queueName); // 绑定队列到交换器 AMQP.Queue.BindOk bindOk = channel.queueBind(queueName, C.FANOUT_EXCHANGE, ""); System.err.println("bindOk:" + bindOk); System.out.println(clientName + " Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(clientName + " Received :" + message + ""); Thread.sleep(1000); } }
public static void t1() throws Exception { Channel channel = generateChannel(); // channel.exchangeDeclare(TestAmqp.ExchangerName, "direct", true, false, null); // channel.exchangeDeclare(TestAmqp.FanoutExchangerName, "fanout", true, false, null); channel.exchangeDeclare(TestAmqp.TopicExchangerName, "topic", false, false, null); channel.queueDeclare(TestAmqp.TopicQueueName, false, false, false, null); // channel.queueBind(TestAmqp.TopicQueueName, TestAmqp.TopicExchangerName, // TestAmqp.RouteingName); // channel.queueBind(TestAmqp.TopicQueueName, TestAmqp.TopicExchangerName, "*"); channel.queueBind(TestAmqp.TopicQueueName, TestAmqp.TopicExchangerName, "#.*"); // channel.basicPublish(TestAmqp.ExchangerName, TestAmqp.RouteingName, null, "haha".getBytes()); QueueingConsumer queueingConsumer = new QueueingConsumer(channel); channel.basicQos(1); channel.basicConsume(TestAmqp.TopicQueueName, queueingConsumer); while (true) { QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery(); System.out.println(new String(delivery.getBody())); long deliveryTag = delivery.getEnvelope().getDeliveryTag(); System.out.println("start basicAck:" + deliveryTag); // if (true)throw new RuntimeException("xxxxx"); channel.basicAck(deliveryTag, false); } // channel.close(); // connection.close(); }
/** * 从指定队列接收消息,然后通过观察者对象处理 * * @param queueName * @param observer * @throws Exception */ public void receive(String queueName, IObserver observer) throws Exception { try { this.init(); // 指定队列 channel.queueDeclare(queueName, true, false, false, null); // 绑定队列到指定的交换机 // channel.queueBind(queueName, mqExchangeName, queueName); // log.info("Bind queue {} to {} success! ", queueName, mqExchangeName); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (true) { // 阻塞,直到接收到一条消息 QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); log.info("Received a message: " + message); observer.update(message); } } finally { this.destory(); } }
@Override protected String doInBackground(Object... params) { Context = (Activity) params[1]; try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("192.168.1.3"); Connection connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare("helloWorld", false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume("helloWorld", true, consumer); while (!isCancelled()) { try { QueueingConsumer.Delivery delivery = consumer.nextDelivery(5000); final String message = new String(delivery.getBody()); final ConsumeData data = new ConsumeData(message); Context.runOnUiThread( new Runnable() { @Override public void run() { TextView mOutput = (TextView) Context.findViewById(R.id.output); mOutput.setMovementMethod(new ScrollingMovementMethod()); for (String item : data.Messages) { mOutput.setText(item + "\n" + mOutput.getText()); } } }); System.out.println(" [x] Received '" + message + "'"); } catch (Exception ex) { } } } catch (Exception ex) { } return ""; }
private void consumeMessages() { while (true) { QueueingConsumer.Delivery delivery; try { delivery = consumer.nextDelivery(); final String message = new String(delivery.getBody()); runOnUiThread( new Runnable() { public void run() { Toast.makeText(ReceiveActivity.this, message, Toast.LENGTH_LONG).show(); } }); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } catch (ShutdownSignalException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (ConsumerCancelledException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public void processFollowings() { while (true) { QueueingConsumer.Delivery delivery; try { delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); Long tag = delivery.getEnvelope().getDeliveryTag(); String[] split = message.split(" "); Long followerVkId = Long.valueOf(split[0]); Long followingVkId = Long.valueOf(split[1]); Follower follower = dbService.getFollowerByVkId(followerVkId); Following following = follower.getFollowingByVkId(followingVkId); refreshFollowing(follower, following); channel.basicAck(tag, false); System.out.println("Ok"); } catch (InterruptedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
public static void main(String[] argv) { Connection connection = null; Channel channel = null; try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String queueName = channel.queueDeclare().getQueue(); if (argv.length < 1) { System.err.println("Usage: ReceiveLogsTopic [binding_key]..."); System.exit(1); } for (String bindingKey : argv) { channel.queueBind(queueName, EXCHANGE_NAME, bindingKey); } System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); String routingKey = delivery.getEnvelope().getRoutingKey(); System.out.println(" [x] Received '" + routingKey + "':'" + message + "'"); } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } }
/** * Emits the next message from the queue as a tuple. * * <p>Serialization schemes returning null will immediately ack and then emit unanchored on the * {@link #ERROR_STREAM_NAME} stream for further handling by the consumer. * * <p>If no message is ready to emit, this will wait a short time ({@link #WAIT_FOR_NEXT_MESSAGE}) * for one to arrive on the queue, to avoid a tight loop in the spout worker. */ @Override public void nextTuple() { if (spoutActive && amqpConsumer != null) { try { final QueueingConsumer.Delivery delivery = amqpConsumer.nextDelivery(WAIT_FOR_NEXT_MESSAGE); if (delivery == null) return; final long deliveryTag = delivery.getEnvelope().getDeliveryTag(); final byte[] message = delivery.getBody(); /////////////////// new rpc BasicProperties props = delivery.getProperties(); BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId()).build(); ////////////////////////////// List<Object> deserializedMessage = serialisationScheme.deserialize(message); if (deserializedMessage != null && deserializedMessage.size() > 0) { // let's see what's inside the Object List (checking) /*System.out.println("Lenght of the list : "+ deserializedMessage.size() +"\n"); for (int i =0; i< deserializedMessage.size(); i++){ Object obj=deserializedMessage.get(i); System.out.println("Object value: "+ obj + "\n" ); }*/ ArrayList li = new ArrayList(); li.add(deserializedMessage.get(0)); li.add(props); li.add(replyProps); deserializedMessage = li; collector.emit(deserializedMessage, deliveryTag); /////////// new for AMQPSpout RPC+JSON try { String response = "your name was: " + deserializedMessage.get(0) + " "; this.amqpChannel.basicPublish( "", props.getReplyTo(), replyProps, response.getBytes("UTF-8")); // this.amqpChannel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); this.amqpChannel.basicAck(deliveryTag, false); } /// publishjson(props, replyProps, delivery.detEnvelope().getDeliveryTag()); // try{ // publishjson(props, replyProps); // } catch (IOException e) { log.error("Error when publishing to the response queue", e); } //////////////// } else { handleMalformedDelivery(deliveryTag, message); } } catch (ShutdownSignalException e) { log.warn("AMQP connection dropped, will attempt to reconnect..."); Utils.sleep(WAIT_AFTER_SHUTDOWN_SIGNAL); reconnect(); } catch (InterruptedException e) { // interrupted while waiting for message, big deal } } }
public static void main(String[] argv) throws java.io.IOException, InterruptedException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); } }
public void run() throws IOException, InterruptedException { final Connection connection = m_connFactory.newConnection(); final Channel channel = connection.createChannel(); try { channel.exchangeDeclare(m_exchangeName, "topic", true); String dataQueue = channel.queueDeclare().getQueue(); channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE.#"); channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE2.#"); channel.queueBind(dataQueue, m_exchangeName, "EXPORT_REPLICATED_TABLE.#"); channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE_FOO.#"); channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE2_FOO.#"); channel.queueBind(dataQueue, m_exchangeName, "EXPORT_REPLICATED_TABLE_FOO.#"); String doneQueue = channel.queueDeclare().getQueue(); channel.queueBind(doneQueue, m_exchangeName, "EXPORT_DONE_TABLE.#"); channel.queueBind(doneQueue, m_exchangeName, "EXPORT_DONE_TABLE_FOO.#"); // Setup callback for data stream channel.basicConsume(dataQueue, false, createConsumer(channel)); // Setup callback for the done message QueueingConsumer doneConsumer = new QueueingConsumer(channel); channel.basicConsume(doneQueue, true, doneConsumer); // Wait until the done message arrives, then verify count final QueueingConsumer.Delivery doneMsg = doneConsumer.nextDelivery(); final long expectedRows = Long.parseLong( ExportOnServerVerifier.RoughCSVTokenizer.tokenize( new String(doneMsg.getBody(), Charsets.UTF_8))[6]); while (expectedRows > m_verifiedRows) { Thread.sleep(1000); System.err.println("Expected " + expectedRows + " " + m_verifiedRows); } } finally { tearDown(channel); channel.close(); connection.close(); } }
public static void t3() throws Exception { Channel channel = generateChannel(); channel.exchangeDeclare(TestAmqp.PingExchangerName, "direct", true, false, null); channel.queueDeclare("rpc", false, false, false, null); // channel.queueBind(TestAmqp.TopicQueueName, TestAmqp.TopicExchangerName, // TestAmqp.RouteingName); // channel.queueBind(TestAmqp.TopicQueueName, TestAmqp.TopicExchangerName, "*"); channel.queueBind("rpc", TestAmqp.PingExchangerName, "ping"); QueueingConsumer queueingConsumer = new QueueingConsumer(channel); channel.basicConsume("rpc", false, queueingConsumer); while (true) { QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery(); System.out.println("log: " + new String(delivery.getBody())); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); System.out.println("reply to :" + delivery.getProperties().getReplyTo()); channel.basicPublish("", delivery.getProperties().getReplyTo(), null, "asfasdfas".getBytes()); } }
public static void t2() throws Exception { Channel channel = generateChannel(); AMQP.Queue.DeclareOk declareOk1 = channel.queueDeclare(); AMQP.Queue.DeclareOk declareOk2 = channel.queueDeclare(); AMQP.Queue.DeclareOk declareOk3 = channel.queueDeclare(); String exchange = "amq.rabbitmq.log"; channel.queueBind(declareOk1.getQueue(), exchange, "error"); channel.queueBind(declareOk2.getQueue(), exchange, "waring"); channel.queueBind(declareOk3.getQueue(), exchange, "info"); QueueingConsumer queueingConsumer = new QueueingConsumer(channel); channel.basicConsume(declareOk1.getQueue(), false, queueingConsumer); channel.basicConsume(declareOk2.getQueue(), false, queueingConsumer); channel.basicConsume(declareOk3.getQueue(), false, queueingConsumer); while (true) { QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery(); System.out.println("log: " + new String(delivery.getBody())); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } }
@Override public void run() { try { Connection connection = factory.newConnection(); String uuid = UUID.randomUUID().toString(); String queueName = RPC_QUEUE_NAME + uuid; Map<String, Channel> result = this.server.initChannel(connection, this.assignedNumber, queueName); Channel consumerChannel = result.get("consumer"); Channel publisherChannel = result.get("publisher"); QueueingConsumer consumer = new QueueingConsumer(consumerChannel); consumerChannel.basicConsume(queueName, true, consumer); System.out.println("in server "); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); // System.out.printf("delivery is %s, body is %s", delivery, new // String(delivery.getBody())); BasicProperties props = delivery.getProperties(); // System.out.printf("correlationId is %s", props.getCorrelationId()); BasicProperties replyProps = new BasicProperties.Builder() .correlationId(props.getCorrelationId()) .messageId(props.getMessageId()) .build(); String response = new String(delivery.getBody()); // System.out.printf("response is %s", new String(delivery.getBody())); // System.out.printf("reply to is %s", props.getReplyTo() ); publisherChannel.basicPublish( PUBLISHER_EXCHANGE_NAME, props.getReplyTo(), replyProps, response.getBytes()); // consumerChannel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } catch (Exception e) { e.printStackTrace(); } }
@Override public void run() { while (true) { Channel ch = null; try { ch = RabbitConnection.getInstance().newChanel(); ch.basicQos(1); AMQP.Queue.DeclareOk q = ch.queueDeclare(); ch.queueBind(q.getQueue(), "amq.direct", routingKey); QueueingConsumer consumer = new QueueingConsumer(ch); ch.basicConsume(q.getQueue(), true, consumer); // Process deliveries while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); Message msg = handler.obtainMessage(); Bundle bundle = new Bundle(); bundle.putString("msg", message); msg.setData(bundle); handler.sendMessage(msg); } } catch (InterruptedException e) { break; } catch (Exception e1) { try { Thread.sleep(INTERVAL_RECOVERY); // sleep and then try again } catch (InterruptedException e) { break; } } finally { RabbitConnection.getInstance().closeChanel(ch); } } }
public void run() { System.out.println(" [x] Awaiting RPC requests"); while (run.get()) { QueueingConsumer.Delivery delivery; try { delivery = consumer.nextDelivery(2000); } catch (ShutdownSignalException e2) { break; } catch (ConsumerCancelledException e2) { break; } catch (InterruptedException e2) { continue; } BasicProperties props = delivery.getProperties(); BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId()).build(); try { Object o; synchronized (registry) { o = Rmivm.deserialise(delivery.getBody(), registry); } byte[] outBytes = Rmivm.serialize(o); synchronized (channel) { channel.basicPublish("", props.getReplyTo(), replyProps, outBytes); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } catch (Exception e) { try { synchronized (channel) { channel.basicPublish( "", props.getReplyTo(), replyProps, Rmivm.serialize(new Exception("Boom!"))); } } catch (IOException e1) { // panic } } } System.out.println(" [x] Shutting Down"); }
public void serveRequests() { while (true) { try { Delivery delivery = consumer.nextDelivery(); BasicProperties props = delivery.getProperties(); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); System.out.println("Received API call...replying..."); channel.basicPublish("", props.getReplyTo(), null, getResponse(delivery).getBytes("UTF-8")); } catch (Exception e) { System.out.println(e.toString()); } } }
@Override public void run() { while (isRunning) { try { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String body = new String(delivery.getBody(), "UTF-8"); Log.d("Aptoide-RabbitMqService", "MESSAGE: " + body); handleMessage(body); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } catch (InterruptedException e) { e.printStackTrace(); } catch (IOException e) { isRunning = false; e.printStackTrace(); } catch (ShutdownSignalException e) { isRunning = false; try { Log.d( "Aptoide-WebInstall", "Connection closed with reason " + e.getReason().toString()); } catch (NullPointerException e1) { e1.printStackTrace(); Log.d("Aptoide-WebInstall", "Connection closed with unkonwn reason"); } } catch (ConsumerCancelledException e) { isRunning = false; Log.d("Aptoide-WebInstall", "Connection was canceled"); } } try { if (channel != null && channel.isOpen()) { channel.close(); connection.disconnectChannel(channel); } if (connection != null && connection.isOpen()) { connection.close(); } } catch (Exception e) { e.printStackTrace(); } }
public void nextTuple() { if (amqpConsumer != null) { try { final QueueingConsumer.Delivery delivery = amqpConsumer.nextDelivery(WAIT_FOR_NEXT_MESSAGE); if (delivery == null) return; final long deliveryTag = delivery.getEnvelope().getDeliveryTag(); final byte[] message = delivery.getBody(); log.debug("Sending message:" + new String(message)); log.debug("Getting next tuple - " + (messageCount++)); if (messageCount < 0) { log.debug("You just get a number of messages = max value of long! Wow! Reseting to 0"); this.messageCount = 0; } collector.emit(serialisationScheme.deserialize(message), deliveryTag); } catch (InterruptedException e) { // interrupted while waiting for message, big deal } } }
public Message nextMessage() { reinitIfNecessary(); if (consumerTag == null || consumer == null) return Message.NONE; try { return Message.forDelivery(consumer.nextDelivery(MS_WAIT_FOR_MESSAGE)); } catch (ShutdownSignalException sse) { reset(); logger.error("shutdown signal received while attempting to get next message", sse); reporter.reportError(sse); return Message.NONE; } catch (InterruptedException ie) { /* nothing to do. timed out waiting for message */ logger.debug("interruepted while waiting for message", ie); return Message.NONE; } catch (ConsumerCancelledException cce) { /* if the queue on the broker was deleted or node in the cluster containing the queue failed */ reset(); logger.error("consumer got cancelled while attempting to get next message", cce); reporter.reportError(cce); return Message.NONE; } }
/** {@inheritDoc} */ @Override public SampleResult sample(Entry entry) { SampleResult result = new SampleResult(); result.setSampleLabel(getName()); result.setSuccessful(false); result.setResponseCode("500"); QueueingConsumer consumer; String consumerTag; trace("AMQPConsumer.sample()"); try { initChannel(); consumer = new QueueingConsumer(channel); channel.basicQos(1); // TODO: make prefetchCount configurable? consumerTag = channel.basicConsume(getQueue(), autoAck(), consumer); } catch (IOException ex) { log.error("Failed to initialize channel", ex); return result; } result.setSampleLabel(getTitle()); /* * Perform the sampling */ result.sampleStart(); // Start timing try { QueueingConsumer.Delivery delivery = consumer.nextDelivery(getReceiveTimeoutAsInt()); if (delivery == null) { log.warn("nextDelivery timed out"); return result; } /* * Set up the sample result details */ result.setSamplerData(new String(delivery.getBody())); result.setResponseData("OK", null); result.setDataType(SampleResult.TEXT); result.setResponseCodeOK(); result.setResponseMessage("OK"); result.setSuccessful(true); if (!autoAck()) channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } catch (ShutdownSignalException e) { log.warn("AMQP consumer failed to consume", e); result.setResponseCode("400"); result.setResponseMessage(e.toString()); interrupt(); } catch (ConsumerCancelledException e) { log.warn("AMQP consumer failed to consume", e); result.setResponseCode("300"); result.setResponseMessage(e.toString()); interrupt(); } catch (InterruptedException e) { log.info("interuppted while attempting to consume"); result.setResponseCode("200"); result.setResponseMessage(e.toString()); } catch (IOException e) { log.warn("AMQP consumer failed to consume", e); result.setResponseCode("100"); result.setResponseMessage(e.toString()); } finally { try { channel.basicCancel(consumerTag); } catch (IOException e) { log.error("Couldn't safely cancel the sample's consumer", e); } } result.sampleEnd(); // End timimg trace("AMQPConsumer.sample ended"); return result; }
@Override public void run() { while (true) { if (closed) { break; } try { connection = connectionFactory.newConnection(rabbitAddresses); channel = connection.createChannel(); } catch (Exception e) { if (!closed) { logger.warn("failed to created a connection / channel", e); } else { continue; } cleanup(0, "failed to connect"); try { Thread.sleep(5000); } catch (InterruptedException e1) { // ignore, if we are closing, we will exit later } } QueueingConsumer consumer = null; // define the queue try { if (rabbitQueueDeclare) { // only declare the queue if we should channel.queueDeclare( rabbitQueue /*queue*/, rabbitQueueDurable /*durable*/, false /*exclusive*/, rabbitQueueAutoDelete /*autoDelete*/, rabbitQueueArgs /*extra args*/); } if (rabbitExchangeDeclare) { // only declare the exchange if we should channel.exchangeDeclare( rabbitExchange /*exchange*/, rabbitExchangeType /*type*/, rabbitExchangeDurable); } channel.basicQos( rabbitQosPrefetchSize /*qos_prefetch_size*/, rabbitQosPrefetchCount /*qos_prefetch_count*/, false); if (rabbitQueueBind) { // only bind queue if we should channel.queueBind( rabbitQueue /*queue*/, rabbitExchange /*exchange*/, rabbitRoutingKey /*routingKey*/); } consumer = new QueueingConsumer(channel); channel.basicConsume(rabbitQueue /*queue*/, false /*noAck*/, consumer); } catch (Exception e) { if (!closed) { logger.warn("failed to create queue [{}]", e, rabbitQueue); } cleanup(0, "failed to create queue"); continue; } // now use the queue to listen for messages while (true) { if (closed) { break; } QueueingConsumer.Delivery task; try { task = consumer.nextDelivery(); } catch (Exception e) { if (!closed) { logger.error("failed to get next message, reconnecting...", e); } cleanup(0, "failed to get message"); break; } if (task != null && task.getBody() != null) { final List<Long> deliveryTags = Lists.newArrayList(); BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); try { processBody(task, bulkRequestBuilder); } catch (Exception e) { logger.warn( "failed to parse request for delivery tag [{}], ack'ing...", e, task.getEnvelope().getDeliveryTag()); try { channel.basicAck(task.getEnvelope().getDeliveryTag(), false); } catch (IOException e1) { logger.warn("failed to ack [{}]", e1, task.getEnvelope().getDeliveryTag()); } continue; } deliveryTags.add(task.getEnvelope().getDeliveryTag()); if (bulkRequestBuilder.numberOfActions() < bulkSize) { // try and spin some more of those without timeout, so we have a bigger bulk (bounded // by the bulk size) try { while ((task = consumer.nextDelivery(bulkTimeout.millis())) != null) { try { processBody(task, bulkRequestBuilder); deliveryTags.add(task.getEnvelope().getDeliveryTag()); } catch (Throwable e) { logger.warn( "failed to parse request for delivery tag [{}], ack'ing...", e, task.getEnvelope().getDeliveryTag()); try { channel.basicAck(task.getEnvelope().getDeliveryTag(), false); } catch (Exception e1) { logger.warn( "failed to ack on failure [{}]", e1, task.getEnvelope().getDeliveryTag()); } } if (bulkRequestBuilder.numberOfActions() >= bulkSize) { break; } } } catch (InterruptedException e) { if (closed) { break; } } catch (ShutdownSignalException sse) { logger.warn( "Received a shutdown signal! initiatedByApplication: [{}], hard error: [{}]", sse, sse.isInitiatedByApplication(), sse.isHardError()); if (!closed && sse.isInitiatedByApplication()) { logger.error("failed to get next message, reconnecting...", sse); } cleanup(0, "failed to get message"); break; } } if (logger.isTraceEnabled()) { logger.trace( "executing bulk with [{}] actions", bulkRequestBuilder.numberOfActions()); } // if we have no bulk actions we might have processed custom commands, so ack them if (ordered || bulkRequestBuilder.numberOfActions() == 0) { try { if (bulkRequestBuilder.numberOfActions() > 0) { BulkResponse response = bulkRequestBuilder.execute().actionGet(); if (response.hasFailures()) { // TODO write to exception queue? logger.warn("failed to execute: " + response.buildFailureMessage()); } } } catch (Exception e) { logger.warn("failed to execute bulk", e); } for (Long deliveryTag : deliveryTags) { try { channel.basicAck(deliveryTag, false); } catch (Exception e1) { logger.warn("failed to ack [{}]", e1, deliveryTag); } } } else { if (bulkRequestBuilder.numberOfActions() > 0) { bulkRequestBuilder.execute( new ActionListener<BulkResponse>() { @Override public void onResponse(BulkResponse response) { if (response.hasFailures()) { // TODO write to exception queue? logger.warn("failed to execute: " + response.buildFailureMessage()); } for (Long deliveryTag : deliveryTags) { try { channel.basicAck(deliveryTag, false); } catch (Exception e1) { logger.warn("failed to ack [{}]", e1, deliveryTag); } } } @Override public void onFailure(Throwable e) { logger.warn( "failed to execute bulk for delivery tags [{}], not ack'ing", e, deliveryTags); } }); } } } } } cleanup(0, "closing river"); }
protected void consumeNoDuplicates(QueueingConsumer consumer) throws ShutdownSignalException, InterruptedException { assertNotNull(consumer.nextDelivery(TIMEOUT)); Delivery markerDelivery = consumer.nextDelivery(TIMEOUT); assertEquals(new String(MARKER), new String(markerDelivery.getBody())); }