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:org.apache.niolex.rabbit.basic.Send.java
License:Apache License
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); for (int i = 0; i < 100; ++i) { String message = String.format("%02d %s", i, MockUtil.randString(5)); ThreadUtil.sleepAtLeast(1000);/*from w ww .j a v a 2 s.c om*/ channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); } channel.close(); connection.close(); }
From source file:org.apache.niolex.rabbit.log.EmitLog.java
License:Apache License
public static void main(String[] argv) throws java.io.IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); for (int i = 0; i < 100; ++i) { String message = String.format("%02d %s", i, MockUtil.randString(5)); ThreadUtil.sleepAtLeast(1000);//from w ww . jav a 2s. co m channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); } channel.close(); connection.close(); }
From source file:org.apache.niolex.rabbit.log.EmitLogDirect.java
License:Apache License
public static void main(String[] argv) throws java.io.IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct"); for (int i = 0; i < 100; ++i) { String message = String.format("%02d %s", i, MockUtil.randString(5)); String severity = MockUtil.randInt(3) == 0 ? "error" : "info"; ThreadUtil.sleepAtLeast(1000);/*www. j av a2 s . com*/ channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes()); System.out.println(" [x] Sent '" + severity + "': '" + message + "'"); } channel.close(); connection.close(); }
From source file:org.apache.niolex.rabbit.log.EmitLogTopic.java
License:Apache License
public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); for (int i = 0; i < 100; ++i) { String message = String.format("%02d %s", i, MockUtil.randString(5)); ThreadUtil.sleepAtLeast(1000);/*from w w w .j a v a 2s . co m*/ String routingKey = getSeverity() + "." + getFacility(); channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes()); System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'"); } channel.close(); connection.close(); }
From source file:org.apache.niolex.rabbit.rpc.RPCServer.java
License:Apache License
public static void main(String[] argv) { Connection connection = null; Channel channel = null; try {/*w ww. ja v a 2 s . c om*/ ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); 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) { String response = null; QueueingConsumer.Delivery delivery = consumer.nextDelivery(); BasicProperties props = delivery.getProperties(); BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId()) .build(); try { String message = new String(delivery.getBody(), "UTF-8"); int n = Integer.parseInt(message); System.out.println(" [.] fib(" + message + ")"); response = "" + fib(n); } catch (Exception e) { System.out.println(" [.] " + e.toString()); response = ""; } finally { channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes("UTF-8")); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } }
From source file:org.apache.nutch.indexer.history.HistoryIndexer.java
License:Apache License
private void sendEvent(final String message) { ConnectionFactory factory = new ConnectionFactory(); final String host = conf.get("history.rabbitmq.host", "localhost"); factory.setHost(host);// w w w . j a v a 2 s . c o m final String user = conf.get("history.rabbitmq.user"); factory.setUsername(user); final String password = conf.get("history.rabbitmq.password"); factory.setPassword(password); Connection connection = null; Channel channel = null; try { connection = factory.newConnection(); channel = connection.createChannel(); } catch (IOException | TimeoutException e) { final String errMsg = "failed opening connection or channel"; LOG.error(errMsg, e); closeChannelAndConnection(channel, true); throw new RuntimeException(errMsg, e); } final String queueNamesStr = conf.get("history.rabbitmq.queue.names", "nlp,langdetector"); final String[] queueNames = queueNamesStr.split(","); for (final String singleQueueName : queueNames) { try { channel.queueDeclare(singleQueueName, true, false, false, null); channel.basicPublish("", singleQueueName, null, message.getBytes()); LOG.debug(" [x] Sent '" + message + "'"); } catch (IOException e) { final String errMsg = String.format("failed sending message [%s] to queue [%s]", message, singleQueueName); LOG.error(errMsg, e); } } closeChannelAndConnection(channel, false); }
From source file:org.apache.synapse.message.store.impl.rabbitmq.RabbitMQProducer.java
License:Open Source License
public boolean storeMessage(MessageContext synCtx) { if (synCtx == null) { return false; }// w w w . ja v a2 s. co m if (connection == null) { if (logger.isDebugEnabled()) { logger.error(getId() + " cannot proceed. RabbitMQ Connection is null."); } logger.warn(getId() + ". Ignored MessageID : " + synCtx.getMessageID()); return false; } StorableMessage message = MessageConverter.toStorableMessage(synCtx); boolean error = false; Throwable throwable = null; Channel channel = null; try { //Serializing message ByteArrayOutputStream os = new ByteArrayOutputStream(); ObjectOutput objOut = new ObjectOutputStream(os); objOut.writeObject(message); byte[] byteForm = os.toByteArray(); objOut.close(); os.close(); //building AMQP message AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder(); builder.messageId(synCtx.getMessageID()); builder.deliveryMode(MessageProperties.MINIMAL_PERSISTENT_BASIC.getDeliveryMode()); builder.priority(message.getPriority(DEFAULT_PRIORITY)); channel = connection.createChannel(); if (exchangeName == null) { channel.basicPublish("", queueName, builder.build(), byteForm); } else { channel.basicPublish(exchangeName, queueName, builder.build(), byteForm); } } catch (IOException e) { throwable = e; error = true; isConnectionError = true; } catch (Throwable t) { throwable = t; error = true; } finally { if (channel != null && channel.isOpen()) try { channel.close(); } catch (IOException e) { logger.error("Error when closing connection" + synCtx.getMessageID() + ". " + e); } } if (error) { String errorMsg = getId() + ". Ignored MessageID : " + synCtx.getMessageID() + ". Could not store message to store [" + store.getName() + "]. Error:" + throwable.getLocalizedMessage(); logger.error(errorMsg, throwable); store.closeProducerConnection(); connection = null; if (logger.isDebugEnabled()) { logger.debug(getId() + ". Ignored MessageID : " + synCtx.getMessageID()); } return false; } if (logger.isDebugEnabled()) { logger.debug(getId() + ". Stored MessageID : " + synCtx.getMessageID()); } store.enqueued(); return true; }
From source file:org.apache.synapse.tranport.amqp.AMQPTwoWayProducerClient.java
License:Apache License
private static void produceAndConsume(String message, Channel channel, String requestQueueName, String replyQueueName) throws IOException, InterruptedException { AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder(); String restCoID = Math.random() + ""; builder.correlationId(restCoID);//from ww w. jav a 2 s . co m System.out.println("Request correlation Id : " + restCoID); builder.replyTo(replyQueueName); channel.basicPublish("", requestQueueName, builder.build(), message.getBytes()); System.out.println("[x] sent to '" + requestQueueName + "' the message '\n" + message + "'"); channel.queueDeclare(replyQueueName, false, false, false, null); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(replyQueueName, true, consumer); System.out.println("Waiting for message on queue '" + replyQueueName + "'"); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String replyMessage = new String(delivery.getBody()); System.out.println("[x] received '" + replyMessage + "'"); System.out.println("Response correlation Id : " + delivery.getProperties().getCorrelationId()); break; } }
From source file:org.archive.modules.AMQPProducer.java
License:Apache License
/** * Publish the message with the supplied properties. If this method returns * without throwing an exception, the message was published successfully. * * @param message/*from w ww . ja v a 2 s . co m*/ * @param props * @throws IOException * if message is not published successfully for any reason */ public void publishMessage(byte[] message, BasicProperties props) throws IOException { Channel channel = channel(); channel.exchangeDeclare(exchange, "direct", true); channel.basicPublish(exchange, routingKey, props, message); }
From source file:org.axonframework.amqp.eventhandling.RabbitMQBenchmark.java
License:Apache License
private static List<Thread> createChannelCreatingThreads(final Connection connection, final String queueName, final boolean transactional) { List<Thread> threads = new ArrayList<>(); for (int i = 0; i < THREAD_COUNT; i++) { threads.add(new Thread(() -> { try { for (int t = 0; t < COMMIT_COUNT; t++) { final Channel localChannel = connection.createChannel(); if (transactional) { localChannel.txSelect(); }/*w w w. ja v a2s . c o m*/ for (int j = 0; j < COMMIT_SIZE; j++) { localChannel.basicPublish("", queueName, null, ("message" + t).getBytes("UTF-8")); } if (transactional) { localChannel.txCommit(); } localChannel.close(); } } catch (IOException | TimeoutException e) { e.printStackTrace(); } })); } return threads; }