List of usage examples for com.rabbitmq.client DefaultConsumer DefaultConsumer
public DefaultConsumer(Channel channel)
From source file:org.graylog2.inputs.amqp.AMQPConsumer.java
License:Open Source License
public Consumer createConsumer(final Channel channel) { return new DefaultConsumer(channel) { @Override//from www.j a va 2 s . c o m public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { try { // The duplication here is a bit unfortunate. Improve by having a Processor Interface. switch (queueConfig.getInputType()) { case GELF: GELFMessage gelf = new GELFMessage(body); try { gelfProcessor.messageReceived(gelf); } catch (BufferOutOfCapacityException e) { LOG.warn("ProcessBufferProcessor is out of capacity. Requeuing message!"); channel.basicReject(envelope.getDeliveryTag(), true); reQueuedMessages.mark(); return; } handledGELFMessages.mark(); break; case SYSLOG: try { syslogProcessor.messageReceived(new String(body), connection.getAddress()); } catch (BufferOutOfCapacityException e) { LOG.warn("ProcessBufferProcessor is out of capacity. Requeuing message!"); channel.basicReject(envelope.getDeliveryTag(), true); reQueuedMessages.mark(); return; } handledSyslogMessages.mark(); break; } channel.basicAck(envelope.getDeliveryTag(), false); handledMessages.mark(); } catch (Exception e) { LOG.error("Could not handle message from AMQP.", e); } } }; }
From source file:org.graylog2.inputs.amqp.Consumer.java
License:Open Source License
public void run() throws IOException { if (!isConnected()) { connect();//from ww w.ja v a 2 s . co m } final MessagePack msgpack = new MessagePack(); channel.basicConsume(queue, false, new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { long deliveryTag = envelope.getDeliveryTag(); try { totalBytesRead.addAndGet(body.length); lastSecBytesReadTmp.addAndGet(body.length); RadioMessage msg = msgpack.read(body, RadioMessage.class); if (!msg.strings.containsKey("message") || !msg.strings.containsKey("source") || msg.timestamp <= 0) { LOG.error("Incomplete AMQP message. Skipping."); channel.basicAck(deliveryTag, false); } Message event = new Message(msg.strings.get("message"), msg.strings.get("source"), new DateTime(msg.timestamp)); event.addStringFields(msg.strings); event.addLongFields(msg.longs); event.addDoubleFields(msg.doubles); processBuffer.insertCached(event, sourceInput); } catch (Exception e) { LOG.error("Error while trying to process AMQP message.", e); } channel.basicAck(deliveryTag, false); } }); }
From source file:org.graylog2.inputs.transports.AmqpConsumer.java
License:Open Source License
public void run() throws IOException { if (!isConnected()) { connect();/*from w ww .j a v a2s .com*/ } for (int i = 0; i < parallelQueues; i++) { final String queueName = String.format(queue, i); channel.queueDeclare(queueName, true, false, false, null); channel.basicConsume(queueName, false, new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { long deliveryTag = envelope.getDeliveryTag(); try { totalBytesRead.addAndGet(body.length); lastSecBytesReadTmp.addAndGet(body.length); final RawMessage rawMessage = new RawMessage(body); // TODO figure out if we want to unsubscribe after a certain time, or if simply blocking is enough here if (amqpTransport.isThrottled()) { amqpTransport.blockUntilUnthrottled(); } sourceInput.processRawMessage(rawMessage); channel.basicAck(deliveryTag, false); } catch (Exception e) { LOG.error("Error while trying to process AMQP message, requeuing message", e); if (channel.isOpen()) { channel.basicNack(deliveryTag, false, true); } } } }); } }
From source file:org.hobbit.core.components.AbstractCommandReceivingComponent.java
License:Open Source License
@Override public void init() throws Exception { super.init(); addCommandHeaderId(getHobbitSessionId()); cmdQueueFactory = new RabbitQueueFactoryImpl(createConnection()); cmdChannel = cmdQueueFactory.getConnection().createChannel(); String queueName = cmdChannel.queueDeclare().getQueue(); cmdChannel.exchangeDeclare(Constants.HOBBIT_COMMAND_EXCHANGE_NAME, "fanout", false, true, null); cmdChannel.queueBind(queueName, Constants.HOBBIT_COMMAND_EXCHANGE_NAME, ""); Consumer consumer = new DefaultConsumer(cmdChannel) { @Override//from ww w. j av a 2s . c o m public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { try { handleCmd(body, properties.getReplyTo()); } catch (Exception e) { LOGGER.error("Exception while trying to handle incoming command.", e); } } }; cmdChannel.basicConsume(queueName, true, consumer); containerName = EnvVariables.getString(Constants.CONTAINER_NAME_KEY, containerName); if (containerName == null) { LOGGER.info("Couldn't get the id of this Docker container. Won't be able to create containers."); } }
From source file:org.hobbit.core.components.AbstractEvaluationStorage.java
License:Open Source License
@Override public void init() throws Exception { super.init(); String queueName = EnvVariables.getString(Constants.TASK_GEN_2_EVAL_STORAGE_QUEUE_NAME_KEY, Constants.TASK_GEN_2_EVAL_STORAGE_DEFAULT_QUEUE_NAME); taskResultReceiver = DataReceiverImpl.builder().maxParallelProcessedMsgs(maxParallelProcessedMsgs) .queue(incomingDataQueueFactory, generateSessionQueueName(queueName)) .dataHandler(new DataHandler() { @Override/*from w w w . ja va 2 s . c o m*/ public void handleData(byte[] data) { ByteBuffer buffer = ByteBuffer.wrap(data); String taskId = RabbitMQUtils.readString(buffer); LOGGER.trace("Received from task generator {}.", taskId); byte[] taskData = RabbitMQUtils.readByteArray(buffer); long timestamp = buffer.getLong(); receiveExpectedResponseData(taskId, timestamp, taskData); } }).build(); queueName = EnvVariables.getString(Constants.SYSTEM_2_EVAL_STORAGE_QUEUE_NAME_KEY, Constants.SYSTEM_2_EVAL_STORAGE_DEFAULT_QUEUE_NAME); final boolean receiveTimeStamp = EnvVariables.getBoolean(RECEIVE_TIMESTAMP_FOR_SYSTEM_RESULTS_KEY, false, LOGGER); final String ackExchangeName = generateSessionQueueName(Constants.HOBBIT_ACK_EXCHANGE_NAME); systemResultReceiver = DataReceiverImpl.builder().maxParallelProcessedMsgs(maxParallelProcessedMsgs) .queue(incomingDataQueueFactory, generateSessionQueueName(queueName)) .dataHandler(new DataHandler() { @Override public void handleData(byte[] data) { ByteBuffer buffer = ByteBuffer.wrap(data); String taskId = RabbitMQUtils.readString(buffer); LOGGER.trace("Received from system {}.", taskId); byte[] responseData = RabbitMQUtils.readByteArray(buffer); long timestamp = receiveTimeStamp ? buffer.getLong() : System.currentTimeMillis(); receiveResponseData(taskId, timestamp, responseData); // If we should send acknowledgments (and there was no // error until now) if (ackChannel != null) { try { ackChannel.basicPublish(ackExchangeName, "", null, RabbitMQUtils.writeString(taskId)); } catch (IOException e) { LOGGER.error("Error while sending acknowledgement.", e); } LOGGER.trace("Sent ack {}.", taskId); } } }).build(); queueName = EnvVariables.getString(Constants.EVAL_MODULE_2_EVAL_STORAGE_QUEUE_NAME_KEY, Constants.EVAL_MODULE_2_EVAL_STORAGE_DEFAULT_QUEUE_NAME); evalModule2EvalStoreQueue = getFactoryForIncomingDataQueues() .createDefaultRabbitQueue(generateSessionQueueName(queueName)); evalModule2EvalStoreQueue.channel.basicConsume(evalModule2EvalStoreQueue.name, true, new DefaultConsumer(evalModule2EvalStoreQueue.channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { byte response[] = null; // get iterator id ByteBuffer buffer = ByteBuffer.wrap(body); if (buffer.remaining() < 1) { response = EMPTY_RESPONSE; LOGGER.error("Got a request without a valid iterator Id. Returning emtpy response."); } else { byte iteratorId = buffer.get(); // get the iterator Iterator<? extends ResultPair> iterator = null; if (iteratorId == NEW_ITERATOR_ID) { // create and save a new iterator iteratorId = (byte) resultPairIterators.size(); LOGGER.info("Creating new iterator #{}", iteratorId); resultPairIterators.add(iterator = createIterator()); } else if ((iteratorId < 0) || iteratorId >= resultPairIterators.size()) { response = EMPTY_RESPONSE; LOGGER.error("Got a request without a valid iterator Id (" + Byte.toString(iteratorId) + "). Returning emtpy response."); } else { iterator = resultPairIterators.get(iteratorId); } if ((iterator != null) && (iterator.hasNext())) { ResultPair resultPair = iterator.next(); Result result = resultPair.getExpected(); byte expectedResultData[], expectedResultTimeStamp[], actualResultData[], actualResultTimeStamp[]; // Make sure that the result is not null if (result != null) { // Check whether the data array is null expectedResultData = result.getData() != null ? result.getData() : new byte[0]; expectedResultTimeStamp = RabbitMQUtils.writeLong(result.getSentTimestamp()); } else { expectedResultData = new byte[0]; expectedResultTimeStamp = RabbitMQUtils.writeLong(0); } result = resultPair.getActual(); // Make sure that the result is not null if (result != null) { // Check whether the data array is null actualResultData = result.getData() != null ? result.getData() : new byte[0]; actualResultTimeStamp = RabbitMQUtils.writeLong(result.getSentTimestamp()); } else { actualResultData = new byte[0]; actualResultTimeStamp = RabbitMQUtils.writeLong(0); } response = RabbitMQUtils.writeByteArrays( new byte[] { iteratorId }, new byte[][] { expectedResultTimeStamp, expectedResultData, actualResultTimeStamp, actualResultData }, null); } else { response = new byte[] { iteratorId }; } } getChannel().basicPublish("", properties.getReplyTo(), null, response); } }); boolean sendAcks = EnvVariables.getBoolean(Constants.ACKNOWLEDGEMENT_FLAG_KEY, false, LOGGER); if (sendAcks) { // Create channel for acknowledgements ackChannel = getFactoryForOutgoingCmdQueues().getConnection().createChannel(); ackChannel.exchangeDeclare(generateSessionQueueName(Constants.HOBBIT_ACK_EXCHANGE_NAME), "fanout", false, true, null); } }
From source file:org.hobbit.core.components.AbstractSequencingTaskGenerator.java
License:Open Source License
@Override public void init() throws Exception { super.init(); // Create channel for incoming acknowledgments using the command // connection (not the data connection!) ackChannel = getFactoryForIncomingCmdQueues().getConnection().createChannel(); String queueName = ackChannel.queueDeclare().getQueue(); String exchangeName = generateSessionQueueName(Constants.HOBBIT_ACK_EXCHANGE_NAME); ackChannel.exchangeDeclare(exchangeName, "fanout", false, true, null); ackChannel.queueBind(queueName, exchangeName, ""); Consumer consumer = new DefaultConsumer(ackChannel) { @Override/*w w w. j av a2 s . c o m*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { try { handleAck(body); } catch (Exception e) { LOGGER.error("Exception while trying to handle incoming command.", e); } } }; ackChannel.basicConsume(queueName, true, consumer); ackChannel.basicQos(1); }
From source file:org.hobbit.core.rabbit.EchoServer.java
License:Open Source License
@Override public void run() { running = true;// w ww . j a v a 2 s . co m Connection connection = null; try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(rabbitHost); connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.basicQos(1); channel.queueDeclare(queueName, false, false, true, null); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { BasicProperties replyProps = new BasicProperties.Builder() .correlationId(properties.getCorrelationId()).deliveryMode(2).build(); channel.basicPublish("", properties.getReplyTo(), replyProps, body); } }; channel.basicConsume(queueName, true, consumer); while (running) { Thread.sleep(3000); } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (IOException e) { } } } }
From source file:org.iplantcollaborative.ClientRegistrar.java
License:Apache License
public void connect() throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(this.serverConf.getHostname()); factory.setPort(this.serverConf.getPort()); factory.setUsername(this.serverConf.getUserId()); factory.setPassword(this.serverConf.getUserPwd()); factory.setVirtualHost(this.serverConf.getVhostPublish()); factory.setAutomaticRecoveryEnabled(true); this.connection = factory.newConnection(); this.channel = this.connection.createChannel(); LOG.info("client registrar connected - " + this.serverConf.getHostname() + ":" + this.serverConf.getPort()); this.channel.basicQos(1); this.channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "#"); this.consumer = new DefaultConsumer(this.channel) { @Override/*w w w . j a va 2s . c o m*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); LOG.info("registration - " + message); BasicProperties replyProps = new BasicProperties.Builder() .correlationId(properties.getCorrelationId()).build(); ARequest request = RequestFactory.getRequestInstance(message); // handle lease request if (request instanceof RequestLease) { ResponseLease res = lease((RequestLease) request); String response_json = serializer.toJson(res); if (properties.getReplyTo() != null) { channel.basicPublish("", properties.getReplyTo(), replyProps, response_json.getBytes()); } else { LOG.error("unable to return response. reply_to field is null"); } } else { LOG.error("Unknown request : " + message); } channel.basicAck(envelope.getDeliveryTag(), false); } }; this.workerThread = new Thread(new Runnable() { @Override public void run() { try { channel.basicConsume(QUEUE_NAME, false, consumer); LOG.info("Waiting for registrations"); } catch (IOException ex) { LOG.error("Exception occurred while consuming message", ex); } } }); this.workerThread.start(); }
From source file:org.iplantcollaborative.DataStoreMessageReceiver.java
License:Apache License
public void connect() throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(this.serverConf.getHostname()); factory.setPort(this.serverConf.getPort()); factory.setUsername(this.serverConf.getUserId()); factory.setPassword(this.serverConf.getUserPwd()); factory.setVirtualHost(this.serverConf.getVhostSubscribe()); factory.setAutomaticRecoveryEnabled(true); this.connection = factory.newConnection(); this.channel = this.connection.createChannel(); LOG.info("subscriber connected - " + this.serverConf.getHostname() + ":" + this.serverConf.getPort()); this.channel.basicQos(1); this.queueName = this.channel.queueDeclare().getQueue(); this.channel.queueBind(this.queueName, EXCHANGE_NAME, "#"); this.consumer = new DefaultConsumer(this.channel) { @Override//from w w w .ja v a2 s. c om public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); LOG.debug("subscribe - " + envelope.getRoutingKey() + ":" + message); DataStoreMessageProcessor processor = binder.getProcessor(); if (processor != null) { processor.process(envelope.getRoutingKey(), message); } else { LOG.error("processor not registered"); } channel.basicAck(envelope.getDeliveryTag(), false); } }; this.workerThread = new Thread(new Runnable() { @Override public void run() { try { channel.basicConsume(queueName, consumer); LOG.info("Waiting for messages"); } catch (IOException ex) { LOG.error("Exception occurred while consuming a message", ex); } } }); this.workerThread.start(); }
From source file:org.mule.transport.amqp.AmqpMessageReceiver.java
License:Open Source License
@Override public void doStart() { try {/*from ww w . jav a 2 s . c om*/ consumerTag = getChannel().basicConsume(getQueueName(), amqpConnector.getAckMode().isAutoAck(), getClientConsumerTag(), amqpConnector.isNoLocal(), amqpConnector.isExclusiveConsumers(), null, new DefaultConsumer(getChannel()) { @Override public void handleDelivery(final String consumerTag, final Envelope envelope, final AMQP.BasicProperties properties, final byte[] body) throws IOException { final AmqpMessage amqpMessage = new AmqpMessage(consumerTag, envelope, properties, body); if (logger.isDebugEnabled()) { logger.debug("Received: " + amqpMessage); } deliverAmqpMessage(amqpMessage); } @Override public void handleShutdownSignal(final String consumerTag, final ShutdownSignalException sse) { if (!sse.isInitiatedByApplication()) { // inform the connector the subscription is dead // so it will reconnect the receiver amqpConnector.handleException(new ConnectException( MessageFactory.createStaticMessage( "Unexpected susbscription shutdown for: " + consumerTag), sse, AmqpMessageReceiver.this)); } } }); logger.info("Started subscription: " + consumerTag + " on channel: " + getChannel()); } catch (final IOException ioe) { throw new MuleRuntimeException( MessageFactory.createStaticMessage( "Error when subscribing to queue: " + getQueueName() + " on channel: " + getChannel()), ioe); } }