List of usage examples for com.rabbitmq.client Envelope getDeliveryTag
public long getDeliveryTag()
From source file:biospectra.classify.server.RabbitMQInputClient.java
License:Apache License
public synchronized void connect() throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); String hostname = this.conf.getRabbitMQHostnames().get(this.hostId); factory.setHost(hostname);/*from w w w. j a v a 2 s. c o m*/ factory.setPort(this.conf.getRabbitMQPort()); factory.setUsername(this.conf.getRabbitMQUserId()); factory.setPassword(this.conf.getRabbitMQUserPwd()); factory.setRequestedHeartbeat(60); factory.setAutomaticRecoveryEnabled(true); this.connection = factory.newConnection(); this.shutdownListener = new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException sse) { LOG.error("connection shutdown", sse); } }; this.connection.addShutdownListener(this.shutdownListener); this.requestChannel = this.connection.createChannel(); this.responseChannel = this.connection.createChannel(); LOG.info("reader connected - " + hostname + ":" + this.conf.getRabbitMQPort()); this.responseChannel.basicQos(10); this.queueName = this.responseChannel.queueDeclare().getQueue(); this.consumer = new DefaultConsumer(this.responseChannel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); this.getChannel().basicAck(envelope.getDeliveryTag(), false); LOG.info("> " + message); ClassificationResponseMessage res = ClassificationResponseMessage.createInstance(message); ClassificationRequest ereq = null; synchronized (requestMap) { ereq = requestMap.get(res.getReqId()); if (ereq != null) { requestMap.remove(res.getReqId()); } } if (ereq == null) { LOG.error("cannot find matching request"); } else { ClassificationResponse eres = new ClassificationResponse(ereq, res); boolean responded = false; synchronized (ereq) { ClassificationRequest.RequestStatus status = ereq.getStatus(); if (status.equals(ClassificationRequest.RequestStatus.STATUS_UNKNOWN)) { ereq.setStatus(ClassificationRequest.RequestStatus.STATUS_RESPONDED); responded = true; } requestQueue.remove(ereq); } if (responded) { LOG.info("res : " + ereq.getReqId()); if (handler != null) { handler.onSuccess(eres.getReqId(), eres.getHeader(), eres.getSequence(), eres.getResult(), eres.getType(), eres.getTaxonRank(), eres.getTaxonName()); } synchronized (requestQueue) { requestQueue.notifyAll(); } } } } }; this.workerThread = new Thread(new Runnable() { @Override public void run() { try { responseChannel.basicConsume(queueName, consumer); LOG.info("Waiting for messages"); } catch (IOException ex) { LOG.error("Exception occurred while consuming a message", ex); } } }); this.workerThread.start(); this.reachable = true; this.timeoutThread = new Thread(new Runnable() { @Override public void run() { while (true) { boolean cont = false; if (requestQueue.size() > 0) { ClassificationRequest ereq = requestQueue.peek(); Date cur = new Date(); if (ereq != null && cur.getTime() - ereq.getSentTime() >= QUERY_TIMEOUT) { LOG.info("found timeout request"); //timeout boolean timeout = false; synchronized (ereq) { ClassificationRequest.RequestStatus status = ereq.getStatus(); if (status.equals(ClassificationRequest.RequestStatus.STATUS_UNKNOWN)) { ereq.setStatus(ClassificationRequest.RequestStatus.STATUS_TIMEOUT); timeout = true; } requestQueue.remove(ereq); } synchronized (requestMap) { requestMap.remove(ereq.getReqId()); } if (timeout) { LOG.info("timeout : " + ereq.getReqId()); handler.onTimeout(ereq.getReqId(), ereq.getHeader(), ereq.getSequence()); synchronized (requestQueue) { requestQueue.notifyAll(); } } cont = true; } } if (!cont) { try { Thread.sleep(1000); } catch (InterruptedException ex) { break; } } } } }); this.timeoutThread.start(); }
From source file:biospectra.classify.server.RabbitMQInputServer.java
License:Apache License
public synchronized void connect() throws IOException, TimeoutException { LOG.info("Connecting to - " + this.conf.getRabbitMQHostname() + ":" + this.conf.getRabbitMQPort()); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(this.conf.getRabbitMQHostname()); factory.setPort(this.conf.getRabbitMQPort()); factory.setUsername(this.conf.getRabbitMQUserId()); factory.setPassword(this.conf.getRabbitMQUserPwd()); factory.setRequestedHeartbeat(60);/*from w ww. ja v a 2 s . c o m*/ factory.setAutomaticRecoveryEnabled(true); this.connection = factory.newConnection(); this.connection.addShutdownListener(new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException sse) { LOG.error("connection shutdown", sse); } }); this.requestChannel = this.connection.createChannel(); this.responseChannel = this.connection.createChannel(); LOG.info("connected."); this.requestChannel.basicQos(1); this.requestChannel.queueDeclare("request", false, false, true, null); this.responseChannel.addReturnListener(new ReturnListener() { @Override public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); LOG.info("message not delivered to " + routingKey); LOG.info(message); } }); this.consumer = new DefaultConsumer(this.requestChannel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); this.getChannel().basicAck(envelope.getDeliveryTag(), false); if (handler != null) { ClassificationRequestMessage req = ClassificationRequestMessage.createInstance(message); handler.handleMessage(req, properties.getReplyTo()); } } }; this.workerThread = new Thread(new Runnable() { @Override public void run() { try { requestChannel.basicConsume("request", consumer); LOG.info("Waiting for messages"); } catch (IOException ex) { LOG.error("Exception occurred while consuming a message", ex); } } }); this.workerThread.start(); }
From source file:cc.gospy.core.remote.rabbitmq.RemoteScheduler.java
License:Apache License
private void declareConsumers() throws IOException { for (String queueName : targetQueueNames) { channel.basicConsume(queueName, false, new DefaultConsumer(channel) { @Override/*from w ww.ja v a 2 s.co m*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { if (isSuspend.get()) { channel.basicNack(envelope.getDeliveryTag(), false, true); } Task task = SerializationUtils.deserialize(body); tasks.put(task, envelope.getDeliveryTag()); checker.schedule(() -> { Task task0 = task; synchronized (tasks) { try { if (tasks.containsKey(task0)) { channel.basicNack(tasks.remove(task0), false, true); logger.warn( "Task {} pending timeout (not taken), re-add to default_task_queue.", task0); } else { synchronized (pendingTasks) { if (pendingTasks.containsKey(task0)) { channel.basicNack(pendingTasks.remove(task0), false, true); logger.warn( "Task {} pending timeout (no feedback), re-add to default_task_queue.", task0); } } } } catch (IOException e) { e.printStackTrace(); } } }, timeoutInSeconds, TimeUnit.SECONDS); } }); } }
From source file:com.abiquo.commons.amqp.consumer.QueueSubscriber.java
License:Open Source License
@Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) { try {/*from ww w . ja v a 2s .c om*/ consumer.consume(envelope, body); } catch (Throwable t) { LOGGER.error("Unhandled exception captured, trying to reject message to prevent consumer crash", t); try { ConsumerUtils.rejectMessage(getChannel(), envelope.getDeliveryTag()); } catch (IOException io) { LOGGER.error("Unable to reject message", io); } } }
From source file:com.abiquo.commons.amqp.consumer.RequestBasedConsumer.java
License:Open Source License
@Override public void consume(Envelope envelope, byte[] body) throws IOException { R request = deserializeRequest(envelope, body); if (request != null) { consume(request, callbacksMap.get(request.getClass())); ackMessage(getChannel(), envelope.getDeliveryTag()); } else {/*ww w .ja v a 2 s . c om*/ rejectMessage(getChannel(), envelope.getDeliveryTag()); } }
From source file:com.abiquo.commons.amqp.impl.am.AMConsumer.java
License:Open Source License
@Override public void consume(Envelope envelope, byte[] body) throws IOException { TemplateStatusEvent event = TemplateStatusEvent.fromByteArray(body); if (event != null) { for (AMCallback callback : callbacks) { if (event.getStatus().equalsIgnoreCase("DOWNLOADING")) { callback.onDownloading(event); } else if (event.getStatus().equalsIgnoreCase("DOWNLOAD")) { callback.onDownload(event); } else if (event.getStatus().equalsIgnoreCase("ERROR")) { callback.onError(event); } else if (event.getStatus().equalsIgnoreCase("NOT_DOWNLOAD")) { callback.onNotDownload(event); } else { throw new IOException("Unexpected OVF Package Instance status : " + event.getStatus()); }//w w w . j av a2s . c o m } ackMessage(getChannel(), envelope.getDeliveryTag()); } else { rejectMessage(getChannel(), envelope.getDeliveryTag()); } }
From source file:com.abiquo.commons.amqp.impl.ha.HAConsumer.java
License:Open Source License
@Override public void consume(Envelope envelope, byte[] body) throws IOException { HATask task = HATask.fromByteArray(body); if (task != null) { for (HACallback callback : callbacks) { callback.executeHighAvailabilityTask(task); }/*from www . jav a2 s . com*/ ackMessage(getChannel(), envelope.getDeliveryTag()); } else { rejectMessage(getChannel(), envelope.getDeliveryTag()); } }
From source file:com.abiquo.commons.amqp.impl.tracer.TracerConsumer.java
License:Open Source License
@Override public void consume(Envelope envelope, byte[] body) throws IOException { Trace trace = Trace.fromByteArray(body); if (trace != null) { for (TracerCallback callback : callbacks) { callback.onTrace(trace);/*from ww w .j a va 2 s . co m*/ } ackMessage(getChannel(), envelope.getDeliveryTag()); } else { rejectMessage(getChannel(), envelope.getDeliveryTag()); } }
From source file:com.abiquo.commons.amqp.impl.vsm.VSMConsumer.java
License:Open Source License
@Override public void consume(Envelope envelope, byte[] body) throws IOException { VirtualSystemEvent event = VirtualSystemEvent.fromByteArray(body); if (event != null) { for (VSMCallback callback : callbacks) { callback.onEvent(event);/*from ww w .j ava2s. c o m*/ } ackMessage(getChannel(), envelope.getDeliveryTag()); } else { rejectMessage(getChannel(), envelope.getDeliveryTag()); } }
From source file:com.analogmountains.flume.RabbitMQSource.java
License:Open Source License
@Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { Map<String, String> headers = eventHeadersFromBasicProperties(properties); headers.put("routingKey", envelope.getRoutingKey()); headers.put("exchange", envelope.getExchange()); Event event = EventBuilder.withBody(body); event.setHeaders(headers);/*from w w w . j a v a 2 s . com*/ getChannelProcessor().processEvent(event); long deliveryTag = envelope.getDeliveryTag(); channel.basicAck(deliveryTag, false); }