List of usage examples for com.rabbitmq.client Channel queueDeclare
Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments) throws IOException;
From source file:com.service.OperationFacadeREST.java
@POST @Override//w w w. ja v a 2 s. co m @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) public void create(Operation entity) { if (!entity.getState().equals("waiting")) { super.create(entity); return; } if (entity.getOperationType())//venda { ArrayList<ClientStock> l = new ArrayList<>(getEntityManager() .find(Client.class, entity.getFkOwnerId().getClientId()).getClientStockCollection()); Boolean fail = false; for (int i = 0; i < l.size(); i++) { if (l.get(i).getStock().equals(entity.getFkStockId())) { if (l.get(i).getQuantity() < entity.getQuantity()) return; l.get(i).setQuantity(l.get(i).getQuantity() - entity.getQuantity()); getEntityManager().persist(l.get(i)); entity.getFkOwnerId().setClientStockCollection(l); entity.setCreationDate(Date.from(Instant.now())); super.create(entity); } } if (fail) return; } else { entity.setCreationDate(Date.from(Instant.now())); if (entity.getFkStockId().getQuantity() > entity.getQuantity()) { System.out.println("yes"); super.create(entity); } else { return; } } try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare("hello", false, false, false, null); System.out.println(super.findAll().get(super.findAll().size() - 1).getOperationId() + "," + entity.getFkOwnerId().getName() + "," + entity.getFkStockId().getName() + "," + entity.getQuantity()); String message = super.findAll().get(super.findAll().size() - 1).getOperationId() + "," + entity.getFkOwnerId().getName() + "," + entity.getFkStockId().getName() + "," + entity.getQuantity(); channel.basicPublish("", "hello", null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close(); } catch (IOException ex) { Logger.getLogger(ClientFacadeREST.class.getName()).log(Level.SEVERE, null, ex); } catch (TimeoutException ex) { Logger.getLogger(ClientFacadeREST.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.shopwiki.roger.QueueUtil.java
License:Apache License
private static DeclareOk declareQueue(Channel channel, String queueName, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> args) throws IOException { if (args == null) { args = Collections.emptyMap(); }/*from w w w.jav a 2 s.com*/ return channel.queueDeclare(queueName, durable, exclusive, autoDelete, args); }
From source file:com.sitewhere.protobuf.test.ActiveMQTests.java
License:Open Source License
@Test public void doRabbitMQTest() throws Exception { String exchangeName = "sitewhere"; String queueName = "SITEWHERE.IN"; String routingKey = "sitewhere"; ConnectionFactory factory = new ConnectionFactory(); factory.setUri("amqp://localhost:5672/SITEWHERE.IN"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(exchangeName, "direct", true); channel.queueDeclare(queueName, true, false, false, null); channel.queueBind(queueName, exchangeName, routingKey); byte[] messageBodyBytes = generateEncodedMeasurementsMessage(); channel.basicPublish(exchangeName, routingKey, null, messageBodyBytes); channel.close();// ww w . j av a 2s. c o m connection.close(); }
From source file:com.sitewhere.sources.ActiveMQTests.java
License:Open Source License
@Test public void doRabbitMQTest() throws Exception { String exchangeName = "sitewhere"; String queueName = "sitewhere.input"; String routingKey = "sitewhere"; ConnectionFactory factory = new ConnectionFactory(); factory.setUri("amqp://localhost:5672"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(exchangeName, "direct", true); channel.queueDeclare(queueName, true, false, false, null); channel.queueBind(queueName, exchangeName, routingKey); byte[] messageBodyBytes = EventsHelper.generateEncodedMeasurementsMessage(HARDWARE_ID); channel.basicPublish(exchangeName, routingKey, null, messageBodyBytes); channel.close();/*w w w. j a v a2 s. c o m*/ connection.close(); }
From source file:com.streamsets.pipeline.lib.rabbitmq.common.RabbitUtil.java
License:Apache License
private static void initRabbitConf(Channel channel, BaseRabbitConfigBean conf) throws IOException { // Channel is always bound to the default exchange. When specified, we must declare the exchange. for (RabbitExchangeConfigBean exchange : conf.exchanges) { channel.exchangeDeclare(exchange.name, exchange.type.getValue(), exchange.durable, exchange.autoDelete, exchange.declarationProperties); }// www .j ava 2 s .co m channel.queueDeclare(conf.queue.name, conf.queue.durable, conf.queue.exclusive, conf.queue.autoDelete, conf.queue.properties); for (RabbitExchangeConfigBean exchange : conf.exchanges) { bindQueue(channel, conf, exchange); } }
From source file:com.trivago.mail.pigeon.web.data.process.QueueNewsletter.java
License:Apache License
private void queueNewsletter(Mail mail, Sender sender, Recipient recipient, Campaign campaign) { Connection conn = ConnectionPool.getConnection(); Channel channel = null; MailTransport transport = templateProcessor.processMail(mail, recipient, sender, campaign); if (transport == null) { log.warn(// w ww . j a v a 2 s . c om "Template processor returned null instead of a mail transport object. This is probably a bug!"); return; } if (transport.shouldAbortSending() && !transport.shouldEnforceSending()) { log.info("Skipped mail to " + transport.getTo() + " because transport aborted sending."); return; } String json = JSON.defaultJSON().forValue(transport); try { channel = conn.createChannel(); channel.exchangeDeclare("mailpidgeon", "direct", true); channel.queueDeclare(channelName, true, false, false, null); channel.queueBind(channelName, "mailpidgeon", "mailpidgeon"); byte[] messageBodyBytes = json.getBytes(); channel.basicPublish("mailpidgeon", "mailpidgeon", null, messageBodyBytes); } catch (IOException e) { log.error(e); } finally { if (channel != null) { try { channel.close(); } catch (IOException e) { log.error("Could not close channel", e); } } } }
From source file:com.UseCaseSimpleConsumer.java
License:Open Source License
public static void main(String[] argv) throws java.io.IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); try {// w w w. ja va 2s. c o m channel.exchangeDeclarePassive(EXCHANGE_NAME); } catch (java.io.IOException e) { if (!channel.isOpen()) channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct"); } try { channel.queueDeclarePassive(ROUTE_KEY); } catch (java.io.IOException e) { if (!channel.isOpen()) channel = connection.createChannel(); channel.queueDeclare(ROUTE_KEY, false, false, false, null); } channel.queueBind(ROUTE_KEY, EXCHANGE_NAME, ROUTE_KEY); String param = "IBM"; String msg = "<m:placeOrder xmlns:m=\"http://services.samples\">\n" + " <m:order>\n" + " <m:price>" + getRandom(100, 0.9, true) + "</m:price>\n" + " <m:quantity>" + (int) getRandom(10000, 1.0, true) + "</m:quantity>\n" + " <m:symbol>" + param + "</m:symbol>\n" + " </m:order>\n" + "</m:placeOrder>"; channel.basicPublish(EXCHANGE_NAME, ROUTE_KEY, new AMQP.BasicProperties.Builder().contentType("text/plain").build(), msg.getBytes()); System.out.println(" [x] Sent '" + msg + "'"); channel.close(); connection.close(); }
From source file:com.UseCaseSimpleProducer.java
License:Open Source License
public static void main(String[] argv) throws java.io.IOException, InterruptedException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); try {//w w w . j a v a 2s .c o m channel.exchangeDeclarePassive(EXCHANGE_NAME); } catch (java.io.IOException e) { channel.exchangeDeclare(EXCHANGE_NAME, "direct"); } try { channel.queueDeclarePassive(QUEUE_NAME); } catch (java.io.IOException e) { channel.queueDeclare(QUEUE_NAME, false, false, false, null); } channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, QUEUE_NAME); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); } }
From source file:com.vmware.bdd.utils.RabbitMQConsumer.java
License:Open Source License
/** * Receive and process each message until the listener indicating. A new * queue will be created when start and will be deleted when stopping * receiving message.//from w w w . java2 s.c om * * FIXME Is it a best practice to create one queue for one task? Or we should * create one thread to handle all messages? * * @param listener * message processor callback * @throws IOException */ public void processMessage(MessageListener listener) throws IOException { ConnectionFactory factory = new ConnectionFactory(); if (username != null && !username.equals("")) { factory.setUsername(username); factory.setPassword(password); } factory.setVirtualHost("/"); factory.setHost(host); factory.setPort(port); Connection conn = factory.newConnection(); Channel channel = conn.createChannel(); /** * make exchange and queue non-durable */ channel.exchangeDeclare(exchangeName, "direct", true); if (!getQueue) { channel.queueDeclare(queueName, false, true, true, null); } else { queueName = channel.queueDeclare().getQueue(); } channel.queueBind(queueName, exchangeName, routingKey); boolean noAck = false; QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, noAck, consumer); while (true) { QueueingConsumer.Delivery delivery; try { delivery = consumer.nextDelivery(mqRecvTimeoutMs); } catch (InterruptedException e) { logger.warn("message consumer interrupted", e); continue; } if (delivery == null) { logger.debug("timeout, no message received"); if (stopping && new Date().after(mqExpireTime)) { logger.error("stop receiving messages without normal termination"); break; } continue; } String message = new String(delivery.getBody()); if (graceStopping) { extendExpirationTime(); } logger.info("message received: " + message); try { if (!listener.onMessage(message)) { logger.info("stop receiving messages normally"); break; } } catch (Throwable t) { logger.error("calling message listener failed", t); // discard and continue in non-debug mode AuAssert.unreachable(); } channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } try { channel.queueDelete(queueName); } catch (AlreadyClosedException e) { logger.error("failed to delete queue: " + queueName, e); } try { channel.close(); } catch (AlreadyClosedException e) { logger.error("failed to close channel, queue: " + queueName, e); } try { conn.close(); } catch (AlreadyClosedException e) { logger.error("failed to close connection, queue: " + queueName, e); } }
From source file:com.wakkir.rabbitmq.basic.Reciever.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"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody(), "UTF-8"); System.out.println(" [x] Received '" + message + "'"); }/* w w w .ja v a 2 s. co m*/ }