List of usage examples for com.rabbitmq.client Channel basicConsume
String basicConsume(String queue, boolean autoAck, Consumer callback) throws IOException;
From source file:in.cs654.chariot.turagraksa.ZooKeeperServer.java
License:Open Source License
public static void main(String[] args) { Connection connection = null; Channel channel; try {/*from ww w . j a va 2 s. c o m*/ final ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST_IP_ADDR); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null); channel.basicQos(1); final QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(RPC_QUEUE_NAME, false, consumer); LOGGER.info("ZooKeeper Server started. Waiting for requests..."); ZooKeeper.startPingEcho(); while (true) { final QueueingConsumer.Delivery delivery = consumer.nextDelivery(); BasicResponse response = new BasicResponse(); BasicRequest request = new BasicRequest(); final AMQP.BasicProperties props = delivery.getProperties(); final AMQP.BasicProperties replyProps = new AMQP.BasicProperties.Builder() .correlationId(props.getCorrelationId()).build(); try { final DatumReader<BasicRequest> avroReader = new SpecificDatumReader<BasicRequest>( BasicRequest.class); decoder = DecoderFactory.get().binaryDecoder(delivery.getBody(), decoder); request = avroReader.read(request, decoder); response = ZooKeeperProcessor.process(request); } catch (Exception e) { e.printStackTrace(); LOGGER.severe("Error in handling request: " + e.getMessage()); response = ResponseFactory.getErrorResponse(request); } finally { baos.reset(); final DatumWriter<BasicResponse> avroWriter = new SpecificDatumWriter<BasicResponse>( BasicResponse.class); encoder = EncoderFactory.get().binaryEncoder(baos, encoder); avroWriter.write(response, encoder); encoder.flush(); channel.basicPublish("", props.getReplyTo(), replyProps, baos.toByteArray()); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } } catch (Exception e) { e.printStackTrace(); LOGGER.severe("Error in RPC server: " + e.getMessage()); } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } }
From source file:info.pancancer.arch3.reporting.Arch3ReportImpl.java
License:Open Source License
@Override public Map<String, Status> getLastStatus() { String queueName = settings.getString(Constants.RABBIT_QUEUE_NAME); final String resultQueueName = queueName + "_results"; String resultsQueue = null;/*from w w w . j a va 2s .c o m*/ Channel resultsChannel = null; synchronized (Arch3ReportImpl.this) { try { // read from resultsChannel = Utilities.setupExchange(settings, resultQueueName); // this declares a queue exchange where multiple consumers get the same convertToResult: // https://www.rabbitmq.com/tutorials/tutorial-three-java.html resultsQueue = Utilities.setupQueueOnExchange(resultsChannel, queueName, "SlackReportBot"); resultsChannel.queueBind(resultsQueue, resultQueueName, ""); QueueingConsumer resultsConsumer = new QueueingConsumer(resultsChannel); resultsChannel.basicConsume(resultsQueue, false, resultsConsumer); int messagesToCache = db.getJobs(JobState.RUNNING).size(); Map<String, Status> cache = new TreeMap<>(); int loop = 0; do { loop++; QueueingConsumer.Delivery delivery = resultsConsumer .nextDelivery(Base.FIVE_SECOND_IN_MILLISECONDS); if (delivery == null) { continue; } String messageFromQueue = new String(delivery.getBody(), StandardCharsets.UTF_8); // now parse it as JSONObj Status status = new Status().fromJSON(messageFromQueue); cache.put(status.getIpAddress(), status); } while (loop < LOOP_LIMIT && cache.size() < messagesToCache); return cache; } catch (IOException | ShutdownSignalException | InterruptedException | TimeoutException | ConsumerCancelledException ex) { throw new RuntimeException(ex); } finally { try { if (resultsQueue != null && resultsChannel != null) { resultsChannel.queueDelete(resultsQueue); } if (resultsChannel != null) { resultsChannel.close(); resultsChannel.getConnection().close(); } } catch (IOException | TimeoutException ex) { System.err.println("Could not close channel"); } } } }
From source file:info.pancancer.arch3.worker.WorkerRunnable.java
License:Open Source License
@Override public void run() { int max = maxRuns; try {/*w w w. j a v a 2 s . com*/ // the VM UUID log.info(" WORKER VM UUID provided as: '" + vmUuid + "'"); // write to // TODO: Add some sort of "local debug" mode so that developers working on their local // workstation can declare the queue if it doesn't exist. Normally, the results queue is // created by the Coordinator. resultsChannel = Utilities.setupExchange(settings, this.resultsQueueName); while (max > 0 || this.endless) { log.debug(max + " remaining jobs will be executed"); log.info(" WORKER IS PREPARING TO PULL JOB FROM QUEUE " + this.jobQueueName); if (!endless) { max--; } // jobChannel needs to be created inside the loop because it is closed inside the loop, and it is closed inside this loop to // prevent pre-fetching. Channel jobChannel = Utilities.setupQueue(settings, this.jobQueueName); if (jobChannel == null) { throw new NullPointerException("jobChannel is null for queue: " + this.jobQueueName + ". Something bad must have happened while trying to set up the queue connections. Please ensure that your configuration is correct."); } QueueingConsumer consumer = new QueueingConsumer(jobChannel); jobChannel.basicConsume(this.jobQueueName, false, consumer); QueueingConsumer.Delivery delivery = consumer.nextDelivery(); log.info(vmUuid + " received " + delivery.getEnvelope().toString()); if (delivery.getBody() != null) { String message = new String(delivery.getBody(), StandardCharsets.UTF_8); if (message.trim().length() > 0) { log.info(" [x] Received JOBS REQUEST '" + message + "' @ " + vmUuid); Job job = new Job().fromJSON(message); Status status = new Status(vmUuid, job.getUuid(), StatusState.RUNNING, Utilities.JOB_MESSAGE_TYPE, "job is starting", this.networkAddress); status.setStderr(""); status.setStdout(""); String statusJSON = status.toJSON(); log.info(" WORKER LAUNCHING JOB"); // greedy acknowledge, it will be easier to deal with lost jobs than zombie workers in hostile OpenStack // environments log.info(vmUuid + " acknowledges " + delivery.getEnvelope().toString()); jobChannel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); // we need to close the channel IMMEDIATELY to complete the ACK. jobChannel.close(); // Close the connection object as well, or the main thread may not exit because of still-open-and-in-use resources. jobChannel.getConnection().close(); WorkflowResult workflowResult = new WorkflowResult(); if (testMode) { workflowResult.setWorkflowStdout("everything is awesome"); workflowResult.setExitCode(0); } else { String seqwareEngine = settings.getString(Constants.WORKER_SEQWARE_ENGINE, Constants.SEQWARE_WHITESTAR_ENGINE); String seqwareSettingsFile = settings.getString(Constants.WORKER_SEQWARE_SETTINGS_FILE); String dockerImage = settings.getString(Constants.WORKER_SEQWARE_DOCKER_IMAGE_NAME); workflowResult = launchJob(statusJSON, job, seqwareEngine, seqwareSettingsFile, dockerImage); } status = new Status(vmUuid, job.getUuid(), workflowResult.getExitCode() == 0 ? StatusState.SUCCESS : StatusState.FAILED, Utilities.JOB_MESSAGE_TYPE, "job is finished", networkAddress); status.setStderr(workflowResult.getWorkflowStdErr()); status.setStdout(workflowResult.getWorkflowStdout()); statusJSON = status.toJSON(); log.info(" WORKER FINISHING JOB"); finishJob(statusJSON); } else { log.info(NO_MESSAGE_FROM_QUEUE_MESSAGE); } // we need to close the channel *conditionally* if (jobChannel.isOpen()) { jobChannel.close(); } // Close the connection object as well, or the main thread may not exit because of still-open-and-in-use resources. if (jobChannel.getConnection().isOpen()) { jobChannel.getConnection().close(); } } else { log.info(NO_MESSAGE_FROM_QUEUE_MESSAGE); } if (endless) { log.info("attempting to reset workspace"); DefaultExecutor executor = new DefaultExecutor(); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); // attempt a cleanup CommandLine cli = new CommandLine("sudo"); List<String> args = new ArrayList<>(Arrays.asList("rm", "-rf", "/datastore/*")); cli.addArguments(args.toArray(new String[args.size()])); executor.execute(cli, resultHandler); // Use the result handler for non-blocking call, so this way we should be able to get updates of // stdout and stderr while the command is running. resultHandler.waitFor(); log.info("exit code for cleanup: " + resultHandler.getExitValue()); } } log.info(" \n\n\nWORKER FOR VM UUID HAS FINISHED!!!: '" + vmUuid + "'\n\n"); // turns out this is needed when multiple threads are reading from the same // queue otherwise you end up with multiple unacknowledged messages being undeliverable to other workers!!! if (resultsChannel != null && resultsChannel.isOpen()) { resultsChannel.close(); resultsChannel.getConnection().close(); } log.debug("result channel open: " + resultsChannel.isOpen()); log.debug("result channel connection open: " + resultsChannel.getConnection().isOpen()); } catch (Exception ex) { log.error(ex.getMessage(), ex); } }
From source file:io.druid.firehose.rabbitmq.RabbitMQFirehoseFactory.java
License:Apache License
@Override public Firehose connect(StringInputRowParser firehoseParser) throws IOException { final StringInputRowParser stringParser = firehoseParser; ConnectionOptions lyraOptions = new ConnectionOptions(this.connectionFactory); Config lyraConfig = new Config().withRecoveryPolicy(new RetryPolicy().withMaxRetries(config.getMaxRetries()) .withRetryInterval(Duration.seconds(config.getRetryIntervalSeconds())) .withMaxDuration(Duration.seconds(config.getMaxDurationSeconds()))); String queue = config.getQueue(); String exchange = config.getExchange(); String routingKey = config.getRoutingKey(); boolean durable = config.isDurable(); boolean exclusive = config.isExclusive(); boolean autoDelete = config.isAutoDelete(); final Connection connection = Connections.create(lyraOptions, lyraConfig); connection.addShutdownListener(new ShutdownListener() { @Override/* ww w. j a va 2s. c om*/ public void shutdownCompleted(ShutdownSignalException cause) { log.warn(cause, "Connection closed!"); } }); final Channel channel = connection.createChannel(); channel.queueDeclare(queue, durable, exclusive, autoDelete, null); channel.queueBind(queue, exchange, routingKey); channel.addShutdownListener(new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException cause) { log.warn(cause, "Channel closed!"); } }); // We create a QueueingConsumer that will not auto-acknowledge messages since that // happens on commit(). final QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queue, false, consumer); return new Firehose() { /** * Storing the latest delivery as a member variable should be safe since this will only be run * by a single thread. */ private Delivery delivery; /** * Store the latest delivery tag to be able to commit (acknowledge) the message delivery up to * and including this tag. See commit() for more detail. */ private long lastDeliveryTag; @Override public boolean hasMore() { delivery = null; try { // Wait for the next delivery. This will block until something is available. delivery = consumer.nextDelivery(); if (delivery != null) { lastDeliveryTag = delivery.getEnvelope().getDeliveryTag(); // If delivery is non-null, we report that there is something more to process. return true; } } catch (InterruptedException e) { // A little unclear on how we should handle this. // At any rate, we're in an unknown state now so let's log something and return false. log.wtf(e, "Got interrupted while waiting for next delivery. Doubt this should ever happen."); } // This means that delivery is null or we caught the exception above so we report that we have // nothing more to process. return false; } @Override public InputRow nextRow() { if (delivery == null) { //Just making sure. log.wtf("I have nothing in delivery. Method hasMore() should have returned false."); return null; } return stringParser.parse(StringUtils.fromUtf8(delivery.getBody())); } @Override public Runnable commit() { // This method will be called from the same thread that calls the other methods of // this Firehose. However, the returned Runnable will be called by a different thread. // // It should be (thread) safe to copy the lastDeliveryTag like we do below and then // acknowledge values up to and including that value. return new Runnable() { // Store (copy) the last delivery tag to "become" thread safe. final long deliveryTag = lastDeliveryTag; @Override public void run() { try { log.info("Acknowledging delivery of messages up to tag: " + deliveryTag); // Acknowledge all messages up to and including the stored delivery tag. channel.basicAck(deliveryTag, true); } catch (IOException e) { log.error(e, "Unable to acknowledge message reception to message queue."); } } }; } @Override public void close() throws IOException { log.info("Closing connection to RabbitMQ"); channel.close(); connection.close(); } }; }
From source file:io.druid.segment.realtime.firehose.RabbitMQFirehoseFactory.java
License:Open Source License
@Override public Firehose connect() throws IOException { String queue = config.getQueue(); String exchange = config.getExchange(); String routingKey = config.getRoutingKey(); boolean durable = config.isDurable(); boolean exclusive = config.isExclusive(); boolean autoDelete = config.isAutoDelete(); final Connection connection = connectionFactory.newConnection(); connection.addShutdownListener(new ShutdownListener() { @Override/* ww w. j av a2s.com*/ public void shutdownCompleted(ShutdownSignalException cause) { log.warn(cause, "Connection closed!"); //FUTURE: we could try to re-establish the connection here. Not done in this version though. } }); final Channel channel = connection.createChannel(); channel.queueDeclare(queue, durable, exclusive, autoDelete, null); channel.queueBind(queue, exchange, routingKey); channel.addShutdownListener(new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException cause) { log.warn(cause, "Channel closed!"); //FUTURE: we could try to re-establish the connection here. Not done in this version though. } }); // We create a QueueingConsumer that will not auto-acknowledge messages since that // happens on commit(). final QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queue, false, consumer); return new Firehose() { /** * Storing the latest delivery as a member variable should be safe since this will only be run * by a single thread. */ private QueueingConsumer.Delivery delivery; /** * Store the latest delivery tag to be able to commit (acknowledge) the message delivery up to * and including this tag. See commit() for more detail. */ private long lastDeliveryTag; @Override public boolean hasMore() { delivery = null; try { // Wait for the next delivery. This will block until something is available. delivery = consumer.nextDelivery(); if (delivery != null) { lastDeliveryTag = delivery.getEnvelope().getDeliveryTag(); // If delivery is non-null, we report that there is something more to process. return true; } } catch (InterruptedException e) { // A little unclear on how we should handle this. // At any rate, we're in an unknown state now so let's log something and return false. log.wtf(e, "Got interrupted while waiting for next delivery. Doubt this should ever happen."); } // This means that delivery is null or we caught the exception above so we report that we have // nothing more to process. return false; } @Override public InputRow nextRow() { if (delivery == null) { //Just making sure. log.wtf("I have nothing in delivery. Method hasMore() should have returned false."); return null; } return parser.parse(new String(delivery.getBody())); } @Override public Runnable commit() { // This method will be called from the same thread that calls the other methods of // this Firehose. However, the returned Runnable will be called by a different thread. // // It should be (thread) safe to copy the lastDeliveryTag like we do below and then // acknowledge values up to and including that value. return new Runnable() { // Store (copy) the last delivery tag to "become" thread safe. final long deliveryTag = lastDeliveryTag; @Override public void run() { try { log.info("Acknowledging delivery of messages up to tag: " + deliveryTag); // Acknowledge all messages up to and including the stored delivery tag. channel.basicAck(deliveryTag, true); } catch (IOException e) { log.error(e, "Unable to acknowledge message reception to message queue."); } } }; } @Override public void close() throws IOException { log.info("Closing connection to RabbitMQ"); channel.close(); connection.close(); } }; }
From source file:it.jugtorino.one.msvc.way.rabbit.geoip.reference.client.RpcClient.java
License:Open Source License
public RpcClient(Channel channel, String queue) throws IOException { this.channel = channel; this.queue = queue; this.callbackQueue = channel.queueDeclare().getQueue(); this.consumer = new QueueingConsumer(channel); channel.basicConsume(callbackQueue, true, consumer); this.deliveryListeners = new ConcurrentHashMap<>(); }
From source file:javarpc_server.JavaRPC_Server.java
/** * @param args the command line arguments * @throws java.io.IOException/*from ww w.j av a 2 s. com*/ * @throws java.lang.InterruptedException */ public static void main(String[] args) throws IOException, InterruptedException { // TODO code application logic here ConnectionFactory factory = new ConnectionFactory(); System.out.println(factory.getUsername() + " " + factory.getPassword()); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null); channel.basicQos(1); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(RPC_QUEUE_NAME, false, consumer); System.out.println(" [x] Awaiting RPC requests"); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); BasicProperties props = delivery.getProperties(); BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId()) .build(); String message = new String(delivery.getBody()); System.out.println(" [.] convert(" + message + ")"); String response = "" + convert(message); channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes()); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } }
From source file:joram.amqp.PersistenceSimpleTest.java
License:Open Source License
public void recover2() throws Exception { ConnectionFactory cnxFactory = new ConnectionFactory(); Connection connection = cnxFactory.newConnection(); Channel channel = connection.createChannel(); DeclareOk declareOk = channel.queueDeclare("testqueue", true, false, false, null); channel.txSelect();/*from w w w .j ava2 s .c o m*/ for (int i = 0; i < 5; i++) { channel.basicPublish("", declareOk.getQueue(), MessageProperties.PERSISTENT_BASIC, "this is a test message !!!".getBytes()); } channel.txCommit(); killAgentServer((short) 0); startAgentServer((short) 0); Thread.sleep(500); connection = cnxFactory.newConnection(); channel = connection.createChannel(); declareOk = channel.queueDeclarePassive("testqueue"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(declareOk.getQueue(), true, consumer); for (int i = 0; i < 5; i++) { Delivery msg = consumer.nextDelivery(1000); assertNotNull(msg); assertEquals("this is a test message !!!", new String(msg.getBody())); } Delivery msg = consumer.nextDelivery(1000); assertNull(msg); channel.queueDelete(declareOk.getQueue()); }
From source file:joram.bridgeamqp.AMQPReceiver.java
License:Open Source License
public static AMQPReceiver createAMQPConsumer(String queue) throws IOException { ConnectionFactory cnxFactory = new ConnectionFactory(); connection = cnxFactory.newConnection(); Channel channel = connection.createChannel(); AMQPReceiver consumer = new AMQPReceiver(channel, queue); channel.basicConsume(queue, true, consumer); return consumer; }
From source file:loanbroker.Aggregator.java
public void reciveFromNormalizer(Hashtable<String, Message> messageFroumBankList, Hashtable<String, Message> messagesFromNormalizer, ArrayList<Message> foundMessages) throws IOException, TimeoutException, Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName);//from www.j a v a 2s . c o m factory.setPort(5672); factory.setUsername("student"); factory.setPassword("cph"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // channel.exchangeDeclare(inputEXCHANGE_NAME, "direct"); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.NormalizerToAggregator); System.out.println(" [*] Waiting for messages on " + ExchangeName.GLOBAL + RoutingKeys.NormalizerToAggregator + ". To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String m = new String(body, "UTF-8"); Gson gson = new GsonBuilder().create(); LoanResponse lp = gson.fromJson(m, LoanResponse.class); Message fm = new Message("" + lp.getSsn(), (int) lp.getInterestRate(), 0, lp.getBank()); messagesFromNormalizer.put(lp.getCorrelationId(), fm); System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + fm.toString() + "'"); try { checkLoanMessages(messageFroumBankList, messagesFromNormalizer, foundMessages); } catch (InterruptedException ex) { Logger.getLogger(Aggregator.class.getName()).log(Level.SEVERE, null, ex); } catch (TimeoutException ex) { Logger.getLogger(Aggregator.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(Aggregator.class.getName()).log(Level.SEVERE, null, ex); } } }; channel.basicConsume(queueName, true, consumer); }