List of usage examples for com.rabbitmq.client Connection close
@Override void close() throws IOException;
From source file:com.mycompany.javateste.queues.pubsub.EmitLog.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.exchangeDeclare(EXCHANGE_NAME, "fanout"); String message = getMessage(argv); channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); channel.close();// w w w. j a v a 2 s .co m connection.close(); }
From source file:com.mycompany.javateste.queues.routing.EmitLogDirect.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.exchangeDeclare(EXCHANGE_NAME, "direct"); String severity = getSeverity(argv); String message = getMessage(argv); channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + severity + "':'" + message + "'"); channel.close();//from w w w . j ava2 s . c o m connection.close(); }
From source file:com.mycompany.javateste.queues.Send.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); String message = "Hello World!"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); channel.close();/* w ww. j a va 2 s.co m*/ connection.close(); }
From source file:com.mycompany.javateste.queues.topic.EmitLogTopic.java
public static void main(String[] argv) { Connection connection = null; Channel channel = null;// www . j av a 2 s .co m try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String routingKey = getRouting(argv); String message = getMessage(argv); channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'"); } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } }
From source file:com.mycompany.javateste.queues.worktasks.NewTask.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(TASK_QUEUE_NAME, true, false, false, null); String message = getMessage(argv); channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); channel.close();// ww w .ja va 2 s. c om connection.close(); }
From source file:com.navercorp.pinpoint.plugin.jdk7.rabbitmq.RabbitMQTestRunner.java
License:Apache License
void runPushTest() throws Exception { final String message = "hello rabbit mq"; // producer side final Connection producerConnection = connectionFactory.newConnection(); final Channel producerChannel = producerConnection.createChannel(); producerChannel.exchangeDeclare(RabbitMQTestConstants.EXCHANGE, "direct", false); producerChannel.queueDeclare(RabbitMQTestConstants.QUEUE_PUSH, false, false, false, null); producerChannel.queueBind(RabbitMQTestConstants.QUEUE_PUSH, RabbitMQTestConstants.EXCHANGE, RabbitMQTestConstants.ROUTING_KEY_PUSH); AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder(); producerChannel.basicPublish(RabbitMQTestConstants.EXCHANGE, RabbitMQTestConstants.ROUTING_KEY_PUSH, false, false, builder.appId("test").build(), message.getBytes()); producerChannel.close();/* ww w . j a v a 2 s. com*/ producerConnection.close(); //comsumer side final Connection consumerConnection = connectionFactory.newConnection(); final Channel consumerChannel = consumerConnection.createChannel(); final String remoteAddress = consumerConnection.getAddress().getHostAddress() + ":" + consumerConnection.getPort(); consumerChannel.queueDeclare(RabbitMQTestConstants.QUEUE_PUSH, false, false, false, null); TestConsumer<String> consumer = new TestConsumer<String>(consumerChannel, MessageConverter.FOR_TEST); consumerChannel.basicConsume(RabbitMQTestConstants.QUEUE_PUSH, true, consumer); // wait consumer Assert.assertEquals(message, consumer.getMessage(10, TimeUnit.SECONDS)); consumerChannel.close(); consumerConnection.close(); PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); // Wait till all traces are recorded (consumer traces are recorded from another thread) awaitAndVerifyTraceCount(verifier, 6, 5000L); verifier.printCache(); Class<?> producerChannelClass = producerChannel.getClass(); Method channelBasicPublish = producerChannelClass.getDeclaredMethod("basicPublish", String.class, String.class, boolean.class, boolean.class, AMQP.BasicProperties.class, byte[].class); ExpectedTrace channelBasicPublishTrace = Expectations.event(RabbitMQTestConstants.RABBITMQ_CLIENT, // serviceType channelBasicPublish, // method null, // rpc remoteAddress, // endPoint "exchange-" + RabbitMQTestConstants.EXCHANGE, // destinationId Expectations.annotation("rabbitmq.exchange", RabbitMQTestConstants.EXCHANGE), Expectations.annotation("rabbitmq.routingkey", RabbitMQTestConstants.ROUTING_KEY_PUSH)); ExpectedTrace rabbitMqConsumerInvocationTrace = Expectations.root(RabbitMQTestConstants.RABBITMQ_CLIENT, // serviceType "RabbitMQ Consumer Invocation", // method "rabbitmq://exchange=" + RabbitMQTestConstants.EXCHANGE, // rpc null, // endPoint (collected but API to retrieve local address is not available in all versions, so skip) remoteAddress, // remoteAddress Expectations.annotation("rabbitmq.routingkey", RabbitMQTestConstants.ROUTING_KEY_PUSH)); Class<?> consumerDispatchClass = Class.forName("com.rabbitmq.client.impl.ConsumerDispatcher"); Method consumerDispatchHandleDelivery = consumerDispatchClass.getDeclaredMethod("handleDelivery", Consumer.class, String.class, Envelope.class, AMQP.BasicProperties.class, byte[].class); ExpectedTrace consumerDispatcherHandleDeliveryTrace = Expectations .event(RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL, consumerDispatchHandleDelivery); // method ExpectedTrace asynchronousInvocationTrace = Expectations.event(ServiceType.ASYNC.getName(), "Asynchronous Invocation"); Class<?> consumerClass = consumer.getClass(); Method consumerHandleDelivery = consumerClass.getDeclaredMethod("handleDelivery", String.class, Envelope.class, AMQP.BasicProperties.class, byte[].class); ExpectedTrace consumerHandleDeliveryTrace = Expectations .event(RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL, consumerHandleDelivery); Class<?> propagationMarkerClass = PropagationMarker.class; Method propagationMarkerMark = propagationMarkerClass.getDeclaredMethod("mark"); ExpectedTrace markTrace = Expectations.event(ServiceType.INTERNAL_METHOD.getName(), propagationMarkerMark); verifier.verifyTrace(channelBasicPublishTrace, rabbitMqConsumerInvocationTrace, consumerDispatcherHandleDeliveryTrace, asynchronousInvocationTrace, consumerHandleDeliveryTrace, markTrace); verifier.verifyTraceCount(0); }
From source file:com.navercorp.pinpoint.plugin.jdk7.rabbitmq.RabbitMQTestRunner.java
License:Apache License
void runPullTest() throws Exception { final String message = "hello rabbit mq"; // producer side final Connection producerConnection = connectionFactory.newConnection(); final Channel producerChannel = producerConnection.createChannel(); producerChannel.exchangeDeclare(RabbitMQTestConstants.EXCHANGE, "direct", false); producerChannel.queueDeclare(RabbitMQTestConstants.QUEUE_PULL, false, false, false, null); producerChannel.queueBind(RabbitMQTestConstants.QUEUE_PULL, RabbitMQTestConstants.EXCHANGE, RabbitMQTestConstants.ROUTING_KEY_PULL); AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder(); producerChannel.basicPublish(RabbitMQTestConstants.EXCHANGE, RabbitMQTestConstants.ROUTING_KEY_PULL, false, false, builder.appId("test").build(), message.getBytes()); producerChannel.close();//from ww w. j a va 2s . co m producerConnection.close(); //comsumer side final Connection consumerConnection = connectionFactory.newConnection(); final Channel consumerChannel = consumerConnection.createChannel(); final String remoteAddress = consumerConnection.getAddress().getHostAddress() + ":" + consumerConnection.getPort(); TestMessagePuller messagePuller = new TestMessagePuller(consumerChannel); Assert.assertEquals(message, messagePuller.pullMessage(MessageConverter.FOR_TEST, RabbitMQTestConstants.QUEUE_PULL, true)); consumerChannel.close(); consumerConnection.close(); PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); // Wait till all traces are recorded (consumer traces are recorded from another thread) awaitAndVerifyTraceCount(verifier, 5, 5000L); verifier.printCache(); // verify producer traces Class<?> producerChannelClass = producerChannel.getClass(); Method channelBasicPublish = producerChannelClass.getDeclaredMethod("basicPublish", String.class, String.class, boolean.class, boolean.class, AMQP.BasicProperties.class, byte[].class); ExpectedTrace channelBasicPublishTrace = Expectations.event(RabbitMQTestConstants.RABBITMQ_CLIENT, // serviceType channelBasicPublish, // method null, // rpc remoteAddress, // endPoint "exchange-" + RabbitMQTestConstants.EXCHANGE, // destinationId Expectations.annotation("rabbitmq.exchange", RabbitMQTestConstants.EXCHANGE), Expectations.annotation("rabbitmq.routingkey", RabbitMQTestConstants.ROUTING_KEY_PULL)); ExpectedTrace rabbitMqConsumerInvocationTrace = Expectations.root(RabbitMQTestConstants.RABBITMQ_CLIENT, // serviceType "RabbitMQ Consumer Invocation", // method "rabbitmq://exchange=" + RabbitMQTestConstants.EXCHANGE, // rpc null, // endPoint (collected but API to retrieve local address is not available in all versions, so skip) remoteAddress, // remoteAddress Expectations.annotation("rabbitmq.routingkey", RabbitMQTestConstants.ROUTING_KEY_PULL)); Class<?> amqChannelClass = Class.forName("com.rabbitmq.client.impl.AMQChannel"); Method handleCompleteInboundCommand = amqChannelClass.getDeclaredMethod("handleCompleteInboundCommand", AMQCommand.class); ExpectedTrace handleCompleteInboundCommandTrace = Expectations.event( RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL, // serviceType handleCompleteInboundCommand); // method verifier.verifyDiscreteTrace(channelBasicPublishTrace, rabbitMqConsumerInvocationTrace, handleCompleteInboundCommandTrace); // verify consumer traces Class<?> consumerChannelClass = consumerChannel.getClass(); Method channelBasicGet = consumerChannelClass.getDeclaredMethod("basicGet", String.class, boolean.class); ExpectedTrace channelBasicGetTrace = Expectations.event(RabbitMQTestConstants.RABBITMQ_CLIENT_INTERNAL, channelBasicGet); Class<?> propagationMarkerClass = PropagationMarker.class; Method propagationMarkerMark = propagationMarkerClass.getDeclaredMethod("mark"); ExpectedTrace markTrace = Expectations.event(ServiceType.INTERNAL_METHOD.getName(), propagationMarkerMark); verifier.verifyDiscreteTrace(channelBasicGetTrace, markTrace); verifier.verifyTraceCount(0); }
From source file:com.nesscomputing.amqp.AmqpUtils.java
License:Apache License
/** * Close a connection./*from ww w. j av a2s . c o m*/ * * @param connection */ public static void closeQuietly(final Connection connection) { if (connection != null && connection.isOpen()) { try { connection.close(); } catch (IOException ioe) { LOG.warnDebug(ioe, "While closing connection"); } } }
From source file:com.pcs.test.amqp.Publisher.java
License:Open Source License
public static void main(String[] args) throws IOException, TimeoutException { System.out.println("starting"); ConnectionFactory factory = new ConnectionFactory(); factory.setHost("pcss-hdop04"); factory.setAutomaticRecoveryEnabled(true); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "first message , hello world"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println("message sent"); channel.close();/* w w w . j a v a 2 s . com*/ connection.close(); }
From source file:com.project.finalproject.Send.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); try {/* w w w.j av a2s . c o m*/ InputStream is = new FileInputStream("hr.json"); String jsontxt = IOUtils.toString(is); JSONObject jsonObject = new JSONObject(jsontxt); JSONArray stream = jsonObject.getJSONArray("stream"); for (int i = 0; i < stream.length(); i++) { String message = stream.getJSONObject(i).getString("value"); channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); TimeUnit.SECONDS.sleep(1); } } catch (FileNotFoundException fe) { fe.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } channel.close(); connection.close(); }