List of usage examples for com.rabbitmq.client Channel basicConsume
String basicConsume(String queue, boolean autoAck, Consumer callback) throws IOException;
From source file:Process.java
public void procesar() { System.out.println("Paso2"); ConnectionFactory factory = new ConnectionFactory(); try {//from w w w .j a v a 2s. com System.out.println("Paso3"); factory.setUri(uri); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume("hello", true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String mensaje = new String(delivery.getBody()); System.out.println(" [x] Received '" + mensaje + "'"); System.out.println("Procesando..." + mensaje + "-" + Calendar.getInstance()); PlanPago pp = dao.leer(new Long(mensaje)); if (pp.getLinea() != null) { //generar cuota List<Cuota> cuotas = aa.generarCuotas(pp.getValor(), pp.getLinea().getTasa(), pp.getPlazo()); pp.setCuotas(cuotas); //calcular nivel de riesgo pp.setNivelRiesgo(calcularNivelRiesgo()); pp.setEstado("Generado"); //guardar cuota dao.actualizar(pp); for (Cuota c : pp.getCuotas()) { c.setIdPlan(pp.getId()); } dao2.insertar(pp.getCuotas()); } System.out.println("Finalizo " + mensaje + "-" + Calendar.getInstance()); } } catch (URISyntaxException ex) { Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex); } catch (KeyManagementException ex) { Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex); } catch (InterruptedException ex) { Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex); } //listenerContainer.setConnectionFactory(rabbitConnectionFactory); //listenerContainer.setQueueNames(rabbitQueue.getName()); // set the callback for message handling /*listenerContainer.setMessageListener(new MessageListener() { public void onMessage(Message message) { final String mensaje = (String) messageConverter.fromMessage(message); // simply printing out the operation, but expensive computation could happen here System.out.println("Received from RabbitMQ: " + mensaje); } }); // set a simple error handler /*listenerContainer.setErrorHandler(new ErrorHandler() { public void handleError(Throwable t) { t.printStackTrace(); } }); // register a shutdown hook with the JVM Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { System.out.println("Shutting down BigOperationWorker"); listenerContainer.shutdown(); } }); // start up the listener. this will block until JVM is killed. listenerContainer.start(); System.out.println("BigOperationWorker started"); */ }
From source file:Rece.java
public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, true, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override//from w w w.j a v a 2s . co m public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); } }; channel.basicConsume(QUEUE_NAME, true, consumer); }
From source file:RecieveMessage.java
public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); 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"); Consumer consumer = new DefaultConsumer(channel) { //@Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); }//from w w w . jav a 2 s. c om }; channel.basicConsume(QUEUE_NAME, true, consumer); }
From source file:ThreadWorkers.java
public void run() { System.out.println("MyThread running"); ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = null; try {// w w w.ja v a 2 s . c o m connection = factory.newConnection(); } catch (IOException ex) { Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex); } Channel channel = null; try { channel = connection.createChannel(); } catch (IOException ex) { Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex); } try { channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); } catch (IOException ex) { Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex); } System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); try { channel.basicQos(1); } catch (IOException ex) { Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex); } QueueingConsumer consumer = new QueueingConsumer(channel); try { channel.basicConsume(TASK_QUEUE_NAME, false, consumer); } catch (IOException ex) { Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex); } while (true) { QueueingConsumer.Delivery delivery = null; try { delivery = consumer.nextDelivery(); } catch (InterruptedException ex) { Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex); } catch (ShutdownSignalException ex) { Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex); } catch (ConsumerCancelledException ex) { Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex); } String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); try { doWork(message); } catch (InterruptedException ex) { Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex); } System.out.println(" [x] Done"); try { channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } catch (IOException ex) { Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:ReplyUtil.java
public void getReply() throws IOException, InterruptedException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(TASK_QUEUE_REPLY_NAME, true, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); channel.basicQos(1);//from ww w . ja v a2 s .c o m QueueingConsumer consumer2 = new QueueingConsumer(channel); channel.basicConsume(TASK_QUEUE_REPLY_NAME, false, consumer2); while (true) { QueueingConsumer.Delivery delivery = consumer2.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Receiving reply from worker::'" + message + "'"); //doWork(message); //System.out.println(" [x] Done" ); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } }
From source file:at.ac.tuwien.dsg.cloud.utilities.messaging.lightweight.rabbitMq.channel.ReceivingChannel.java
License:Apache License
private void reconnect() { if (shutdown.get()) { return;//from w w w . j a v a2s .c o m } try { Thread.sleep(1000); } catch (InterruptedException ex) { return; } this.queueName = null; try { Channel channel = this.getChannel(); this.queueName = channel.queueDeclare().getQueue(); this.consumer = this.rabbitMqFactory.getQueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); synchronized (types) { for (String type : types) { this.internalBindType(type); } } } catch (ChannelException | IOException | IllegalStateException ex) { this.reconnect(); } }
From source file:bank.OurRabbitBank.java
public static void main(String[] args) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.OUR_JSON_BANK); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); AMQP.BasicProperties properties = delivery.getProperties(); String message = new String(delivery.getBody()); Gson g = new Gson(); Message msg = g.fromJson(message, Message.class); System.out.println(" [x] Received '" + message + "'"); sendToNormalizer(msg, properties); }/*from w w w . ja v a2 s .c om*/ }
From source file:blocker.Blocker.java
/** * @param argv/*from ww w . ja v a2 s. c o m*/ */ public static void main(String[] argv) throws Exception { seconds = Integer.parseInt(argv[7]); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(argv[0]); factory.setUsername(argv[2]); factory.setPassword(argv[3]); factory.setVirtualHost(argv[1]); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(argv[4], "direct", true); String queueName = channel.queueDeclare(argv[5], true, false, false, null).getQueue(); // exchange key channel.queueBind(queueName, argv[4], argv[6]); System.out.println(" [*] Waiting for messages. 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 message = new String(body, "UTF-8"); JSONParser parser = new JSONParser(); Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread th, Throwable ex) { System.out.println("Uncaught exception: " + ex); } }; try { Object obj = parser.parse(message); JSONObject jobj = (JSONObject) obj; String IP = (String) jobj.get("clientip"); Thread t = new Thread(new BlockerThread(IP)); t.setUncaughtExceptionHandler(h); t.start(); } catch (ParseException ex) { Logger.getLogger(Blocker.class.getName()).log(Level.SEVERE, null, ex); } } }; channel.basicConsume(argv[5], true, consumer); }
From source file:brooklyn.entity.messaging.rabbit.RabbitEc2LiveTest.java
License:Apache License
@Override protected void doTest(Location loc) throws Exception { RabbitBroker rabbit = app.createAndManageChild(EntitySpec.create(RabbitBroker.class)); rabbit.start(ImmutableList.of(loc)); EntityTestUtils.assertAttributeEqualsEventually(rabbit, RabbitBroker.SERVICE_UP, true); byte[] content = "MessageBody".getBytes(Charsets.UTF_8); String queue = "queueName"; Channel producer = null;// www. java 2s . c om 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(); } }
From source file:brooklyn.entity.messaging.rabbit.RabbitIntegrationTest.java
License:Apache License
/** * Test that an AMQP client can connect to and use the broker. *///from www . j a v a 2 s .c o m @Test(groups = { "Integration", "WIP" }) public void testClientConnection() throws Exception { rabbit = app.createAndManageChild(EntitySpec.create(RabbitBroker.class)); rabbit.start(ImmutableList.of(testLocation)); EntityTestUtils.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); } }