List of usage examples for com.rabbitmq.client Connection createChannel
Channel createChannel() throws IOException;
From source file:ch.icclab.cyclops.consume.ConsumerEntry.java
License:Open Source License
public ConsumerEntry(AbstractConsumer cl, Connection con, String q, int p) throws Exception { clazz = cl;// ww w . jav a 2 s. c o m queue = q; prefetch = p; // create channel and declare queues channel = con.createChannel(); channel.queueDeclare(queue, true, false, false, null); // create consumer consumer = clazz.handleDelivery(this); // set prefetch limit channel.basicQos(prefetch, false); // start consuming and save the tag tag = channel.basicConsume(queue, false, consumer); }
From source file:co.edu.uniandes.cloud.simuladorcredito.negocio.Process.java
public void procesar() { ConnectionFactory factory = new ConnectionFactory(); try {/*w w w .j a v a 2 s .c om*/ 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:co.lp.arch.TestUtils.java
static void spawnThreadWithNewChannel(ConnectionFactory factory, ConsumeWithEx<Channel> f) { new Thread(() -> { try {/*from w ww . ja va 2 s .c om*/ Connection conn = factory.newConnection(); Channel chan = conn.createChannel(); f.accept(chan); chan.close(); conn.close(); } catch (Exception ex) { throw new RuntimeException(ex); } }).start(); }
From source file:Colas.Colas.java
private String reciver(String enviar) { try {/*ww w . j a v a 2 s .com*/ ConnectionFactory factory = new ConnectionFactory(); factory.setHost(IP); factory.setPort(5672); factory.setUsername("valencia"); factory.setPassword("admin123"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare("SC", false, false, false, null); channel.basicPublish("", "SC", null, enviar.getBytes()); System.out.println(" [x] Sent '" + enviar + "'"); channel.close(); connection.close(); } catch (Exception e) { System.out.println("Error enviando "); e.printStackTrace(); } try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); factory.setUsername("guest"); factory.setPassword("admin123"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare("CF", false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume("CF", true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); return message; } } catch (Exception e) { System.out.println("Error reciviendo "); e.printStackTrace(); } return "Error"; }
From source file:Colas.Colas.java
private void sender(String string) { try {//w ww. j a v a 2 s .c om ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); factory.setPort(5672); factory.setUsername("valencia"); factory.setPassword("admin123"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare("SC", false, false, false, null); channel.basicPublish("", "SC", null, string.getBytes()); System.out.println(" [x] Sent '" + string + "'"); channel.close(); connection.close(); } catch (Exception e) { System.out.println("Error enviando "); e.printStackTrace(); } }
From source file:Colas.Colas.java
@Override public void run() { try {/*from w w w .j a va 2s . c o m*/ ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); factory.setUsername("guest"); factory.setPassword("admin123"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare("CF", false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume("CF", true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); String[] resp = message.split(";"); String p = resp[0]; if (p.equals("Q1")) { pregunta1(resp[1]); } if (p.equals("Q2")) { pregunta2(resp[1]); } if (p.equals("Q3")) { pregunta3(resp[1]); } if (p.equals("Q4")) { pregunta4(resp[1]); } } } catch (Exception e) { System.out.println("Error reciviendo "); e.printStackTrace(); } }
From source file:com.addthis.hydra.job.minion.Minion.java
License:Apache License
private synchronized boolean connectToRabbitMQ() { String[] routingKeys = { uuid, HostMessage.ALL_HOSTS }; batchControlProducer = new RabbitMessageProducer("CSBatchControl", batchBrokerAddresses, batchBrokerUsername, batchBrokerPassword); queryControlProducer = new RabbitMessageProducer("CSBatchQuery", batchBrokerAddresses, batchBrokerUsername, batchBrokerPassword);//from w w w . j av a 2 s. com try { Connection connection = RabbitMQUtil.createConnection(batchBrokerAddresses, batchBrokerUsername, batchBrokerPassword); channel = connection.createChannel(); channel.exchangeDeclare("CSBatchJob", "direct"); AMQP.Queue.DeclareOk result = channel.queueDeclare(uuid + ".batchJob", true, false, false, null); String queueName = result.getQueue(); channel.queueBind(queueName, "CSBatchJob", uuid); channel.queueBind(queueName, "CSBatchJob", HostMessage.ALL_HOSTS); batchJobConsumer = new RabbitQueueingConsumer(channel); channel.basicConsume(queueName, false, batchJobConsumer); batchControlConsumer = new RabbitMessageConsumer(channel, "CSBatchControl", uuid + ".batchControl", Minion.this, routingKeys); return true; } catch (IOException e) { log.error("Error connecting to rabbitmq at {}", batchBrokerAddresses, e); return false; } }
From source file:com.addthis.hydra.job.Minion.java
License:Apache License
private synchronized boolean connectToRabbitMQ() { String[] routingKeys = new String[] { uuid, HostMessage.ALL_HOSTS }; batchControlProducer = new RabbitMessageProducer("CSBatchControl", batchBrokerHost, Integer.valueOf(batchBrokerPort)); queryControlProducer = new RabbitMessageProducer("CSBatchQuery", batchBrokerHost, Integer.valueOf(batchBrokerPort)); try {/*from ww w.j av a 2 s . co m*/ Connection connection = RabbitMQUtil.createConnection(batchBrokerHost, Integer.valueOf(batchBrokerPort)); channel = connection.createChannel(); channel.exchangeDeclare("CSBatchJob", "direct"); AMQP.Queue.DeclareOk result = channel.queueDeclare(uuid + ".batchJob", true, false, false, null); String queueName = result.getQueue(); channel.queueBind(queueName, "CSBatchJob", uuid); channel.queueBind(queueName, "CSBatchJob", HostMessage.ALL_HOSTS); batchJobConsumer = new RabbitQueueingConsumer(channel); channel.basicConsume(queueName, false, batchJobConsumer); batchControlConsumer = new RabbitMessageConsumer(channel, "CSBatchControl", uuid + ".batchControl", this, routingKeys); return true; } catch (IOException e) { log.error("Error connecting to rabbitmq " + batchBrokerHost + ":" + batchBrokerPort, e); return false; } }
From source file:com.addthis.hydra.job.spawn.SpawnMQImpl.java
License:Apache License
@Override public void connectToMQ(String hostUUID) throws IOException { final MessageListener<HostState> hostStateListener = SpawnMQImpl.this::onMessage; QuiesceOnRabbitMQBlockedListener blockedListener = new QuiesceOnRabbitMQBlockedListener(spawn); hostStatusConsumer = new ZkMessageConsumer<>(zkClient, "/minion", hostStateListener, HostState.class); batchJobProducer = RabbitMessageProducer.constructAndOpen("CSBatchJob", batchBrokeAddresses, batchBrokerUsername, batchBrokerPassword, blockedListener); batchControlProducer = RabbitMessageProducer.constructAndOpen("CSBatchControl", batchBrokeAddresses, batchBrokerUsername, batchBrokerPassword, blockedListener); Connection connection = RabbitMQUtil.createConnection(batchBrokeAddresses, batchBrokerUsername, batchBrokerPassword);/*from w w w. ja v a 2 s .c o m*/ channel = connection.createChannel(); batchControlConsumer = new RabbitMessageConsumer<>(channel, "CSBatchControl", hostUUID + Minion.batchControlQueueSuffix, this, ImmutableList.of("SPAWN"), ImmutableList.of(), CoreMessage.class); }
From source file:com.addthis.hydra.job.SpawnMQImpl.java
License:Apache License
@Override public void connectToMQ(String hostUUID) { hostStatusConsumer = new ZkMessageConsumer<>(zkClient, "/minion", this, HostState.class); batchJobProducer = new RabbitMessageProducer("CSBatchJob", "localhost"); batchControlProducer = new RabbitMessageProducer("CSBatchControl", batchBrokerHost, Integer.valueOf(batchBrokerPort)); try {/* w w w .j a v a 2 s. c om*/ Connection connection = RabbitMQUtil.createConnection(batchBrokerHost, Integer.valueOf(batchBrokerPort)); channel = connection.createChannel(); batchControlConsumer = new RabbitMessageConsumer(channel, "CSBatchControl", hostUUID + ".batchControl", this, "SPAWN"); } catch (IOException e) { log.error("Exception connection to broker: " + batchBrokerHost + ":" + batchBrokerPort, e); } }