com.rabbitmq.client.QueueingConsumer Java Examples
The following examples show how to use
com.rabbitmq.client.QueueingConsumer.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RabbitMQConsumer.java From storm-rabbitmq with MIT License | 6 votes |
public void open() { try { connection = createConnection(); channel = connection.createChannel(); if (prefetchCount > 0) { logger.info("setting basic.qos / prefetch count to " + prefetchCount + " for " + queueName); channel.basicQos(prefetchCount); } // run any declaration prior to queue consumption declarator.execute(channel); consumer = new QueueingConsumer(channel); consumerTag = channel.basicConsume(queueName, isAutoAcking(), consumer); } catch (Exception e) { reset(); logger.error("could not open listener on queue " + queueName); reporter.reportError(e); } }
Example #2
Source File: AMQPWorkflowConsumer2.java From reactive with Creative Commons Zero v1.0 Universal | 6 votes |
public void execute() throws Exception { Channel channel = AMQPCommon.connect(); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume("trade.eq.q", true, consumer); QueueingConsumer.Delivery msg = null; while (true) { try { msg = consumer.nextDelivery(); String message = new String(msg.getBody()); System.out.println("received: " + message); String[] parts = message.split(","); long shares = new Long(parts[2]).longValue(); Thread.sleep(1000); } catch (Exception e) { System.out.println("error with trade: " + e.getMessage()); System.out.println("sending to workflow"); channel.basicPublish("", "workflow.q", null, msg.getBody()); } } }
Example #3
Source File: AMQPReceiver.java From reactive with Creative Commons Zero v1.0 Universal | 6 votes |
public static void main(String[] args) throws Exception { Channel channel = AMQPCommon.connect(); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicQos(1); channel.basicConsume("trade.eq.q", false, consumer); int numMsgs = args.length > 0 ? new Integer(args[0]).intValue() : 1; for (int i=0; i<numMsgs; i++) { QueueingConsumer.Delivery msg = consumer.nextDelivery(); System.out.println("received: " + new String(msg.getBody())); Thread.sleep(1000); channel.basicAck(msg.getEnvelope().getDeliveryTag(), false); } AMQPCommon.close(channel); }
Example #4
Source File: AMQPConsumer.java From reactive with Creative Commons Zero v1.0 Universal | 6 votes |
public void execute(String mode) throws Exception { Channel channel = AMQPCommon.connect(); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume("trade.request.q", true, consumer); if (mode.equalsIgnoreCase("stable")) {lower = 300; upper = 1200;} if (mode.equalsIgnoreCase("better")) {lower = 300; upper = 800;} if (mode.equalsIgnoreCase("worse")) {lower = 800; upper = 1900;} if (mode.equalsIgnoreCase("erratic")) {lower = 200; upper = 5000;} while (true) { QueueingConsumer.Delivery message = consumer.nextDelivery(); String msg = new String(message.getBody()); System.out.println("trade order received: " + msg); int response = lower + (int) (Math.random() * (upper-lower)); System.out.println("trade placed, duration = " + response); String newMsg = "response"; byte[] bmsg = newMsg.getBytes(); Thread.sleep(response); channel.basicPublish("", "trade.response.q", null, bmsg); } }
Example #5
Source File: RPCClient.java From rabbitmq with GNU General Public License v2.0 | 6 votes |
public String call(String message) throws Exception { String response = null; String corrId = UUID.randomUUID().toString(); BasicProperties props = new BasicProperties.Builder().correlationId(corrId).replyTo(replyQueueName).build(); channel.basicPublish("", requestQueueName, props, message.getBytes("UTF-8")); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); if (delivery.getProperties().getCorrelationId().equals(corrId)) { response = new String(delivery.getBody(), "UTF-8"); break; } } return response; }
Example #6
Source File: RabbitMqReceiver.java From tangyuan2 with GNU General Public License v3.0 | 6 votes |
private void startSyncReceiveThread(final QueueingConsumer consumer, final boolean autoAck, final BindingVo binding) { syncReceiveThread = new SyncReceiveThread() { @Override public void run() { log.info("start listen to the " + typeStr + "[" + queue.getName() + "]."); while (running) { try { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); XCO xcoMessage = getMessage(delivery.getBody()); log.info("received a message from " + typeStr + "[" + queue.getName() + "]: " + xcoMessage); boolean result = exec(service, xcoMessage, binding); if (!autoAck && result) { channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } catch (ShutdownSignalException e) { // TODO 可能会出现断链的问题 e.printStackTrace(); } catch (Throwable e) { log.error("listen to the [" + queue.getName() + "] error.", e); } } closed = true; } }; syncReceiveThread.start(); }
Example #7
Source File: ClientReceive1.java From java-study with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws java.io.IOException,java.lang.InterruptedException, TimeoutException, KeyManagementException, NoSuchAlgorithmException, URISyntaxException{ ConnectionFactory factory=new ConnectionFactory(); // factory.setHost("localhost"); // factory.setVirtualHost("my_mq"); // factory.setUsername("zhxia"); // factory.setPassword("123456"); factory.setUri("amqp://guest:[email protected]:5672");//获取url Connection connection=factory.newConnection(); Channel channel=connection.createChannel(); channel.queueDeclare(queue_name, durable, false, false, null); System.out.println("Wait for message"); channel.basicQos(1); //消息分发处理 QueueingConsumer consumer=new QueueingConsumer(channel); channel.basicConsume(queue_name, autoAck, consumer); while(true){ Thread.sleep(500); QueueingConsumer.Delivery deliver=consumer.nextDelivery(); String message=new String(deliver.getBody()); System.out.println("Message received:"+message); channel.basicAck(deliver.getEnvelope().getDeliveryTag(), false); } }
Example #8
Source File: RabbitConsumer.java From java-study with Apache License 2.0 | 6 votes |
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 ); } }
Example #9
Source File: RabbitMqManager.java From AuTe-Framework with Apache License 2.0 | 6 votes |
@Override public Message waitMessage(String queueName, Long timeoutMs, String testIdHeaderName, String testId) throws JMSException { try { Channel channel = senderConnection.createChannel(); final QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); QueueingConsumer.Delivery delivery = consumer.nextDelivery(timeoutMs); if (delivery != null) { channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); RMQTextMessage message = new RMQTextMessage(); message.setText(new String(delivery.getBody())); return message; } return null; } catch (IOException | InterruptedException e) { log.error("RabbitMQ waitMessage error: {}", e); } return null; }
Example #10
Source File: Message.java From storm-rabbitmq with MIT License | 6 votes |
private DeliveredMessage(QueueingConsumer.Delivery delivery) { super(delivery.getBody()); redelivery = delivery.getEnvelope().isRedeliver(); deliveryTag = delivery.getEnvelope().getDeliveryTag(); routingKey = delivery.getEnvelope().getRoutingKey(); exchange = delivery.getEnvelope().getExchange(); className = delivery.getProperties().getClassName(); clusterId = delivery.getProperties().getClusterId(); contentEncoding = delivery.getProperties().getContentEncoding(); contentType = delivery.getProperties().getContentType(); correlationId = delivery.getProperties().getCorrelationId(); deliveryMode = delivery.getProperties().getDeliveryMode(); expiration = delivery.getProperties().getExpiration(); headers = delivery.getProperties().getHeaders(); messageId = delivery.getProperties().getMessageId(); priority = delivery.getProperties().getPriority(); replyTo = delivery.getProperties().getReplyTo(); timestamp = delivery.getProperties().getTimestamp(); type = delivery.getProperties().getType(); userId = delivery.getProperties().getUserId(); }
Example #11
Source File: CacheInvalidationSubscriber.java From carbon-commons with Apache License 2.0 | 6 votes |
private void subscribe() { log.debug("Global cache invalidation: initializing the subscription"); try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(ConfigurationManager.getProviderUrl()); int port = Integer.parseInt(ConfigurationManager.getProviderPort()); factory.setPort(port); factory.setUsername(ConfigurationManager.getProviderUsername()); factory.setPassword(ConfigurationManager.getProviderPassword()); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(ConfigurationManager.getTopicName(), "topic"); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, ConfigurationManager.getTopicName(), "#"); consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); Thread reciever = new Thread(messageReciever); reciever.start(); log.info("Global cache invalidation is online"); } catch (Exception e) { log.error("Global cache invalidation: Error message broker initialization", e); } }
Example #12
Source File: CacheInvalidationSubscriber.java From carbon-commons with Apache License 2.0 | 5 votes |
@Override public void run() { while(consumer != null) { try { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); onMessage(delivery.getBody()); } catch (Exception e) { log.error("Global cache invalidation: error message recieve.", e); } } }
Example #13
Source File: AMQPWorkflowProcessor.java From reactive with Creative Commons Zero v1.0 Universal | 5 votes |
public void execute() throws Exception { Channel channel = AMQPCommon.connect(); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume("workflow.q", true, consumer); while (true) { QueueingConsumer.Delivery message = consumer.nextDelivery(); String msg = new String(message.getBody()); System.out.println("received: " + msg); String newMsg = msg.substring(0, msg.indexOf(" shares")); byte[] bmsg = newMsg.getBytes(); System.out.println("Trade fixed: " + newMsg); channel.basicPublish("", "trade.eq.q", null, bmsg); } }
Example #14
Source File: AMQPContProducer.java From reactive with Creative Commons Zero v1.0 Universal | 5 votes |
private void startListener() { new Thread(()-> { try { Channel channel = connection.createChannel(); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicQos(1); channel.basicConsume("flow.q", false, consumer); while (true) { QueueingConsumer.Delivery msg = consumer.nextDelivery(); boolean controlFlow = new Boolean(new String(msg.getBody())).booleanValue(); channel.basicAck(msg.getEnvelope().getDeliveryTag(), false); if (controlFlow) { System.out.println("notification received to slow down..."); synchronized(delay) { delay = 3000l; } } else { System.out.println("system back to normal..."); synchronized(delay) { delay = 1000l; } } } } catch (Exception e) { e.printStackTrace(); } }).start(); }
Example #15
Source File: AMQPContConsumer.java From reactive with Creative Commons Zero v1.0 Universal | 5 votes |
public static void main(String[] args) throws Exception { Channel channel = AMQPCommon.connect(); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicQos(1); channel.basicConsume("trade.eq.q", false, consumer); while (true) { QueueingConsumer.Delivery msg = consumer.nextDelivery(); System.out.println("received: " + new String(msg.getBody())); Thread.sleep(2000); channel.basicAck(msg.getEnvelope().getDeliveryTag(), false); } }
Example #16
Source File: RabbitReader.java From bluima with Apache License 2.0 | 5 votes |
@Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); try { // setup connection ConnectionFactory factory = new ConnectionFactory(); if (amqpUri == null) { factory.setHost("localhost"); } else { factory.setUri(amqpUri); } Connection connection = factory.newConnection(); LOG.info(" [RabbitReader] connected to Rabbit"); // receiving // setup channels receiveChannel = connection.createChannel(); receiveChannel.queueDeclare(queue, DURABLE, false, false, null); receiveChannel.basicQos(1); // max 1 msg at a time to each slave // receiving consumer = new QueueingConsumer(receiveChannel); receiveChannel.basicConsume(queue, false, consumer); LOG.debug(" [RabbitReader] Waiting for messages..."); } catch (Exception e) { throw new ResourceInitializationException(e); } }
Example #17
Source File: RPCClient.java From rabbitmq with GNU General Public License v2.0 | 5 votes |
public RPCClient() throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); replyQueueName = channel.queueDeclare().getQueue(); consumer = new QueueingConsumer(channel); channel.basicConsume(replyQueueName, true, consumer); }
Example #18
Source File: ChatRabbitMQConnector.java From barterli_android with Apache License 2.0 | 5 votes |
/** * Create Exchange and then start consuming. A binding needs to be added * before any messages will be delivered */ public boolean connectToRabbitMQ(final String userName, final String password, final String queueName, final boolean durable, final boolean exclusive, final boolean autoDelete, final Map<String, Object> args) { if (super.connectToRabbitMQ(userName, password)) { try { Logger.d(TAG, "Connected"); mQueue = declareQueue(queueName, durable, exclusive, autoDelete, args); mSubscription = new QueueingConsumer(mChannel); mChannel.basicConsume(mQueue, true, mSubscription); //mChannel.basicQos(1); if (mExchangeType == ExchangeType.FANOUT) { addBinding("");// fanout has default binding } } catch (final IOException e) { e.printStackTrace(); return false; } setIsRunning(true); mHandler.post(mConsumeRunner); return true; } return false; }
Example #19
Source File: ScoreMarkerMain.java From ExamStack with GNU General Public License v2.0 | 5 votes |
@Override public void init(DaemonContext arg0) throws DaemonInitException, Exception { context = new AnnotationConfigApplicationContext(); context.register(ScoreMarkConfig.class); context.refresh(); scoreCalcuService = context.getBean(ScoreCalcuService.class); consumer = context.getBean(QueueingConsumer.class); mapper = context.getBean(ObjectMapper.class); createWorkThread(); LOGGER.info("ScoreMarker daemon init done."); }
Example #20
Source File: ScoreMarkerWin.java From ExamStack with GNU General Public License v2.0 | 5 votes |
public void init() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(ScoreMarkConfig.class); context.refresh(); scoreCalcuService = context.getBean(ScoreCalcuService.class); consumer = context.getBean(QueueingConsumer.class); mapper = context.getBean(ObjectMapper.class); LOGGER.info("ScoreMarker daemon init done."); }
Example #21
Source File: ScoreMarkConfig.java From ExamStack with GNU General Public License v2.0 | 5 votes |
@Bean QueueingConsumer queueingConsumer() throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(messageQueueHostname); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(Constants.ANSWERSHEET_DATA_QUEUE, true, false, false, null); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(Constants.ANSWERSHEET_DATA_QUEUE, true, consumer); return consumer; }
Example #22
Source File: RMQSource.java From flink with Apache License 2.0 | 5 votes |
@Override public void open(Configuration config) throws Exception { super.open(config); try { connection = setupConnection(); channel = connection.createChannel(); if (channel == null) { throw new RuntimeException("None of RabbitMQ channels are available"); } setupQueue(); consumer = new QueueingConsumer(channel); RuntimeContext runtimeContext = getRuntimeContext(); if (runtimeContext instanceof StreamingRuntimeContext && ((StreamingRuntimeContext) runtimeContext).isCheckpointingEnabled()) { autoAck = false; // enables transaction mode channel.txSelect(); } else { autoAck = true; } LOG.debug("Starting RabbitMQ source with autoAck status: " + autoAck); channel.basicConsume(queueName, autoAck, consumer); } catch (IOException e) { throw new RuntimeException("Cannot create RMQ connection with " + queueName + " at " + rmqConnectionConfig.getHost(), e); } this.schema.open(() -> getRuntimeContext().getMetricGroup().addGroup("user")); running = true; }
Example #23
Source File: ScoreMarkerMain.java From ExamStack with GNU General Public License v2.0 | 5 votes |
@Override public void init(DaemonContext arg0) throws DaemonInitException, Exception { context = new AnnotationConfigApplicationContext(); context.register(ScoreMarkConfig.class); context.refresh(); scoreCalcuService = context.getBean(ScoreCalcuService.class); consumer = context.getBean(QueueingConsumer.class); mapper = context.getBean(ObjectMapper.class); createWorkThread(); LOGGER.info("ScoreMarker daemon init done."); }
Example #24
Source File: ScoreMarkerWin.java From ExamStack with GNU General Public License v2.0 | 5 votes |
public void init() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(ScoreMarkConfig.class); context.refresh(); scoreCalcuService = context.getBean(ScoreCalcuService.class); consumer = context.getBean(QueueingConsumer.class); mapper = context.getBean(ObjectMapper.class); LOGGER.info("ScoreMarker daemon init done."); }
Example #25
Source File: ScoreMarkConfig.java From ExamStack with GNU General Public License v2.0 | 5 votes |
@Bean QueueingConsumer queueingConsumer() throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(messageQueueHostname); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(Constants.ANSWERSHEET_DATA_QUEUE, true, false, false, null); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(Constants.ANSWERSHEET_DATA_QUEUE, true, consumer); return consumer; }
Example #26
Source File: RabbitIntegrationTest.java From brooklyn-library with Apache License 2.0 | 5 votes |
/** * 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)); EntityAsserts.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); } }
Example #27
Source File: TradeReader.java From ThriftBook with Apache License 2.0 | 5 votes |
public static void main(String[] argv) throws java.io.IOException, java.lang.InterruptedException, java.util.concurrent.TimeoutException, TException, TTransportException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, true, consumer); System.out.println("Waiting for trade reports..."); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); byte[] data = delivery.getBody(); TMemoryBuffer trans = new TMemoryBuffer(data.length); trans.write(data, 0, data.length); TCompactProtocol proto = new TCompactProtocol(trans); TradeReport tr = new TradeReport(); tr.read(proto); System.out.println("[" + tr.seq_num + "] " + tr.symbol + " @ " + tr.price + " x " + tr.size); } }
Example #28
Source File: RabbitEc2LiveTest.java From brooklyn-library with Apache License 2.0 | 5 votes |
@Override protected void doTest(Location loc) throws Exception { RabbitBroker rabbit = app.createAndManageChild(EntitySpec.create(RabbitBroker.class)); rabbit.start(ImmutableList.of(loc)); EntityAsserts.assertAttributeEqualsEventually(rabbit, RabbitBroker.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, Maps.<String,Object>newHashMap()); 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(); assertEquals(delivery.getBody(), content); } finally { if (producer != null) producer.close(); if (consumer != null) consumer.close(); } }
Example #29
Source File: RMQSource.java From flink with Apache License 2.0 | 5 votes |
@Override public void run(SourceContext<OUT> ctx) throws Exception { final RMQCollector collector = new RMQCollector(ctx); while (running) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); synchronized (ctx.getCheckpointLock()) { if (!autoAck) { final long deliveryTag = delivery.getEnvelope().getDeliveryTag(); if (usesCorrelationId) { final String correlationId = delivery.getProperties().getCorrelationId(); Preconditions.checkNotNull(correlationId, "RabbitMQ source was instantiated " + "with usesCorrelationId set to true but a message was received with " + "correlation id set to null!"); if (!addId(correlationId)) { // we have already processed this message continue; } } sessionIds.add(deliveryTag); } schema.deserialize(delivery.getBody(), collector); if (collector.isEndOfStreamSignalled()) { this.running = false; return; } } } }
Example #30
Source File: ChunkingAmqpReader.java From kieker with Apache License 2.0 | 5 votes |
@Override public Outcome onInitialization() { try { // Prepare the connection, channel and consumer this.connection = this.createConnection(); this.channel = this.connection.createChannel(); this.consumer = new QueueingConsumer(this.channel); return Outcome.SUCCESS; } catch (final KeyManagementException | NoSuchAlgorithmException | TimeoutException | URISyntaxException | IOException e) { this.handleInitializationError(e); return Outcome.FAILURE; } }