use of com.rabbitmq.client.QueueingConsumer in project microservices by pwillhan.
the class CompetingReceiver method receive.
public String receive() {
if (channel == null) {
initialize();
}
String message = null;
try {
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, true, consumer);
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
message = new String(delivery.getBody());
LOGGER.info("Message received: " + message);
return message;
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
} catch (ShutdownSignalException e) {
LOGGER.error(e.getMessage(), e);
} catch (ConsumerCancelledException e) {
LOGGER.error(e.getMessage(), e);
} catch (InterruptedException e) {
LOGGER.error(e.getMessage(), e);
}
return message;
}
use of com.rabbitmq.client.QueueingConsumer in project pancm_project by xuwujing.
the class Worker method main.
// private static final String TASK_QUEUE_NAME = "tsk.hybris.productbrand.tsk";
/**
* The entry point of application.
*
* @param argv the input arguments
* @throws Exception the exception
*/
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
// factory.setHost("localhost");
factory.setUri("amqp://guest:guest@172.26.129.3:5672");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// queue的持久化需要在声明时指定durable=True
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
// 保证在接收端一个消息没有处理完时不会接收另一个消息
channel.basicQos(1);
// channel.basicQos(0, 1, false); //这样RabbitMQ就会使得每个Consumer在同一个时间点最多处理一个Message。换句话说,在接收到该Consumer的ack前,他它不会将新的Message分发给它。
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(TASK_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);
}
}
use of com.rabbitmq.client.QueueingConsumer in project pancm_project by xuwujing.
the class RabbitConsumer method main.
/**
* The entry point of application.
*
* @param argv the input arguments
* @throws Exception the exception
*/
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
// 打开连接和创建频道,与发送端一样
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) {
// 消费者程序运行开着 如果生产者新增了数据会自动获取
Thread.sleep(500);
// nextDelivery是一个阻塞方法(内部实现其实是阻塞队列的take方法)
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println("'[x] Received '" + message);
}
}
use of com.rabbitmq.client.QueueingConsumer in project storm-amqp-spout by rapportive-oss.
the class AMQPSpout method setupAMQP.
private void setupAMQP() throws IOException {
final int prefetchCount = this.prefetchCount;
final ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost(amqpHost);
connectionFactory.setPort(amqpPort);
connectionFactory.setUsername(amqpUsername);
connectionFactory.setPassword(amqpPassword);
connectionFactory.setVirtualHost(amqpVhost);
this.amqpConnection = connectionFactory.newConnection();
this.amqpChannel = amqpConnection.createChannel();
log.info("Setting basic.qos prefetch-count to " + prefetchCount);
amqpChannel.basicQos(prefetchCount);
final Queue.DeclareOk queue = queueDeclaration.declare(amqpChannel);
final String queueName = queue.getQueue();
log.info("Consuming queue " + queueName);
this.amqpConsumer = new QueueingConsumer(amqpChannel);
this.amqpConsumerTag = amqpChannel.basicConsume(queueName, false, /* no auto-ack */
amqpConsumer);
}
use of com.rabbitmq.client.QueueingConsumer in project rabbitmq-java-client by rabbitmq.
the class MemoryAlarms method flowControl.
@Test
public void flowControl() throws IOException, InterruptedException {
basicPublishVolatile(Q);
setResourceAlarm("memory");
// non-publish actions only after an alarm should be fine
assertNotNull(basicGet(Q));
QueueingConsumer c = new QueueingConsumer(channel);
String consumerTag = channel.basicConsume(Q, true, c);
// publishes after an alarm should not go through
basicPublishVolatile(Q);
// the publish is async, so this is racy. This also tests we don't die
// by heartbeat (3x heartbeat interval + epsilon)
assertNull(c.nextDelivery(3100));
// once the alarm has cleared the publishes should go through
clearResourceAlarm("memory");
assertNotNull(c.nextDelivery(3100));
// everything should be back to normal
channel.basicCancel(consumerTag);
basicPublishVolatile(Q);
assertNotNull(basicGet(Q));
}
Aggregations