List of usage examples for com.rabbitmq.client Channel basicPublish
void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException;
From source file:loanbroker.RecipientList.java
private static void sendMessage(String exchangeName, String routingKey, String msg, String corrId) { RabbitConnection rabbitConnection = new RabbitConnection(); Channel channel = rabbitConnection.makeConnection(); try {/* w w w .j a v a2 s.com*/ //set correlationId for Aggregator AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().correlationId(corrId).build(); channel.exchangeDeclare(exchangeName, "direct"); channel.basicPublish(exchangeName, routingKey, props, msg.getBytes("UTF-8")); rabbitConnection.closeChannelAndConnection(); System.out.println(" [x] Sent :" + routingKey + " " + msg + ""); } catch (IOException ex) { System.out.println("Error in RecipientList class - sendToTranslator()"); System.out.println(ex.getStackTrace()); } }
From source file:localdomain.localhost.RabbitMQServer.java
License:Apache License
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {//w ww . j a v a2 s .com String message = request.getParameter("message"); ConnectionFactory factory = new ConnectionFactory(); String uri = System.getProperty("CLOUDAMQP_URL"); factory.setUri(uri); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); boolean durable = true; channel.queueDeclare(QUEUE_NAME, durable, false, false, null); channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close(); response.sendRedirect(request.getContextPath() + "/index.jsp"); } catch (Exception e) { request.setAttribute("throwable", e); request.getRequestDispatcher("/index.jsp").forward(request, response); } }
From source file:main.TestMain.java
public static void sendMessage(String message) throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); factory.setUsername("student"); factory.setPassword("cph"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); channel.close();//from w w w. ja v a 2 s. c o m connection.close(); }
From source file:messaging.Worker.java
License:Apache License
public static void sendMessage(String message) throws java.io.IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(ConfigurationLoader.getInstance().getRabbitmqNodename()); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); channel.close();/* w ww .j a v a 2 s .c o m*/ connection.close(); }
From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java
License:Apache License
/** * MQ ?? ?./*from ww w. jav a 2 s . c o m*/ * <p> * exchange = fanout * ??? topic ? * * @param topic * @param message ? * @param confirm ?? * @return ???rabbit confirm ????? */ public boolean doPublish(String topic, String message, boolean confirm) { Connection connection = rabbitAdapter.getConnection(); Channel channel = connection.createChannel(false); Object funResult = null; try { if (confirm) { channel.confirmSelect(); } channel.exchangeDeclare(topic, BuiltinExchangeType.FANOUT, true); AMQP.BasicProperties properties = new AMQP.BasicProperties("text/plain", null, getMQHeader(topic), 2, 0, null, null, null, null, null, null, null, null, null); funResult = sendBeforeFun.invoke(topic, "", properties); channel.basicPublish(topic, "", properties, message.getBytes()); if (confirm) { try { return channel.waitForConfirms(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); logger.error("[MQ] Rabbit publish error.", e); sendErrorFun.invoke(e, funResult); return false; } } else { return true; } } catch (IOException e) { logger.error("[MQ] Rabbit publish error.", e); sendErrorFun.invoke(e, funResult); return false; } finally { try { channel.close(); sendFinishFun.invoke(funResult); } catch (IOException | TimeoutException e) { logger.error("[MQ] Rabbit publish error.", e); } connection.close(); } }
From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java
License:Apache License
/** * MQ ?? .//from w w w. java2 s .c om * <p> * exchange = fanout * * @param address ? * @param message ? * @param confirm ?? * @return ??rabbit confirm ????? */ public boolean doRequest(String address, String message, boolean confirm) { Connection connection = rabbitAdapter.getConnection(); Channel channel = connection.createChannel(false); Object funResult = null; try { if (confirm) { channel.confirmSelect(); } channel.queueDeclare(address, true, false, false, null); AMQP.BasicProperties properties = new AMQP.BasicProperties("text/plain", null, getMQHeader(address), 2, 0, null, null, null, null, null, null, null, null, null); funResult = sendBeforeFun.invoke("", address, properties); channel.basicPublish("", address, properties, message.getBytes()); if (confirm) { try { return channel.waitForConfirms(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); logger.error("[MQ] Rabbit request error.", e); sendErrorFun.invoke(e, funResult); return false; } } else { return true; } } catch (IOException e) { logger.error("[MQ] Rabbit request error.", e); sendErrorFun.invoke(e, funResult); return false; } finally { try { channel.close(); sendFinishFun.invoke(funResult); } catch (IOException | TimeoutException e) { logger.error("[MQ] Rabbit request error.", e); } connection.close(); } }
From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java
License:Apache License
/** * MQ ?? ?./*from w ww . j a v a 2s . com*/ * <p> * exchange = topic * * @param topic * @param routingKey Key * @param queueName ?? * @param message ? * @param confirm ?? * @return ???rabbit confirm ????? */ public boolean publishWithTopic(String topic, String routingKey, String queueName, String message, boolean confirm) { logger.trace("[MQ] publishWithTopic {}:{}", topic, message); Connection connection = rabbitAdapter.getConnection(); Channel channel = connection.createChannel(false); Object funResult = null; try { if (confirm) { channel.confirmSelect(); } channel.queueDeclare(queueName, true, false, false, null); channel.exchangeDeclare(topic, BuiltinExchangeType.TOPIC, true); AMQP.BasicProperties properties = new AMQP.BasicProperties("text/plain", null, getMQHeader(topic), 2, 0, null, null, null, null, null, null, null, null, null); funResult = sendBeforeFun.invoke(topic, routingKey, properties); channel.basicPublish(topic, routingKey, properties, message.getBytes()); if (confirm) { try { return channel.waitForConfirms(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); logger.error("[MQ] Rabbit publishWithTopic error.", e); sendErrorFun.invoke(e, funResult); return false; } } else { return true; } } catch (IOException e) { logger.error("[MQ] Rabbit publishWithTopic error.", e); sendErrorFun.invoke(e, funResult); return false; } finally { try { channel.close(); sendFinishFun.invoke(funResult); } catch (IOException | TimeoutException e) { logger.error("[MQ] Rabbit publishWithTopic error.", e); } connection.close(); } }
From source file:net.es.netshell.rabbitmq.Box.java
License:Open Source License
public String queryQueue(String sendQueue, String token, Channel channel, String symLink) throws Exception { // Create random UUID for producer's temporary queue String uuid = UUID.randomUUID().toString(); // Declare this temporary queue and start listening (exclusive queue). channel.queueDeclare(uuid, false, true, true, null); QueueingConsumer consumer = new QueueingConsumer(channel); // Send TOKEN_REQUEST with curent username. String message = token + ":QUEUE_QUERY" + ":" + uuid + ":" + KernelThread.currentKernelThread().getUser().getName() + ":" + symLink; channel.basicPublish("", sendQueue, null, message.getBytes()); // Start consuming to receive token. channel.basicConsume(uuid, true, "tokenRequest", false, true, null, consumer); QueueingConsumer.Delivery delivery = consumer.nextDelivery(); // When token is received, store in "token." String queueName = new String(delivery.getBody()); // Delete temporary queue channel.queueDelete(uuid);//from w w w .j av a 2 s . com return queueName; }
From source file:net.es.netshell.rabbitmq.Box.java
License:Open Source License
public void responseQuery(String responseQueue, Channel channel, String symLink, String userName) throws Exception { Queue queryQueue = getQueue(symLink); if (queryQueue.hasPermission(userName)) { String message = queryQueue.getQueueName(); channel.basicPublish("", responseQueue, null, message.getBytes()); } else {//from w w w . jav a2 s. com throw new Exception("User does not have access to this queue"); } }
From source file:net.es.netshell.rabbitmq.CreateToken.java
License:Open Source License
public CreateToken(BrokerInfo info, Channel tokenChannel, String listenerID) throws Exception { // Info on data needed to create a connection this.info = info; // Create random UUID for producer's temporary queue String uuid = UUID.randomUUID().toString(); // Declare this temporary queue and start listening (exclusive queue). tokenChannel.queueDeclare(uuid, false, true, true, null); QueueingConsumer consumer = new QueueingConsumer(tokenChannel); // Send TOKEN_REQUEST with current username. String message = "TOKEN_REQUEST" + ":" + uuid + ":" + KernelThread.currentKernelThread().getUser().getName(); tokenChannel.basicPublish("", listenerID, null, message.getBytes()); // Start consuming to receive token. tokenChannel.basicConsume(uuid, true, "tokenRequest", false, false, null, consumer); QueueingConsumer.Delivery delivery = consumer.nextDelivery(); // When token is received, store in "token." token = new String(delivery.getBody()); // Delete temporary queue tokenChannel.queueDelete(uuid);//from w ww .j ava 2 s . co m }