List of usage examples for com.rabbitmq.client Connection createChannel
Channel createChannel() throws IOException;
From source file:org.opennaas.extensions.genericnetwork.capability.nclprovisioner.components.NetworkObservationsPusher.java
License:Apache License
private void sendStatistics(String topicName, String csvMessage) throws KeyManagementException, NoSuchAlgorithmException, URISyntaxException, IOException { Connection conn = null; Channel channel = null;/* ww w. j a v a2s. c o m*/ try { log.debug("Establishing RabbitMQ connection with sla manager with URI: " + slaManagerUri); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(slaManagerUri.getHost()); conn = factory.newConnection(); channel = conn.createChannel(); channel.exchangeDeclare(topicName, "direct"); log.debug("Publishing message in RabbitMQ channel."); channel.basicPublish(topicName, RABBIT_MQ_ROUTING_KEY, new AMQP.BasicProperties.Builder().contentType(OBSERVATIONS_CONTENT_TYPE).build(), csvMessage.getBytes()); log.debug("Message successfully sent to SLA manager."); } finally { if (channel != null && channel.isOpen()) channel.close(); if (conn != null && conn.isOpen()) conn.close(); log.debug("Connection to RabbitMQ server closed."); } }
From source file:org.pushtrigger.mule.agent.CreateQueueAgent.java
License:Apache License
@Override public void start() throws MuleException { System.out.println("Create Queue Agent is running..."); Connection connection = null; Channel channel = null;// w w w . ja va2 s . co m try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "Hello World! I am creating a queue..."; Map<String, Object> props = new HashMap<String, Object>(); props.put("path", "queue"); props.put("branch", "create"); AMQP.BasicProperties.Builder bob = new AMQP.BasicProperties.Builder(); AMQP.BasicProperties basicProps = bob.headers(props).build(); channel.basicPublish("", QUEUE_NAME, basicProps, message.getBytes()); System.out.println("Agent has created the queue..."); } catch (IOException e) { System.out.println("Something wrong " + e); } finally { try { if (channel != null) channel.close(); } catch (IOException e) { } try { if (connection != null) connection.close(); } catch (IOException e) { } } }
From source file:org.s23m.cell.repository.client.connector.RepositoryClientConnector.java
License:Mozilla Public License
private void initClient() { final ConnectionFactory cfconn = new ConnectionFactory(); cfconn.setHost(ConfigValues.getString("RepositoryClientServer.HOST_NAME")); cfconn.setPort(Integer.parseInt(ConfigValues.getString("RepositoryClientServer.PORT"))); cfconn.setVirtualHost(ConfigValues.getString("RepositoryClientServer.VHOST_NAME")); cfconn.setUsername(ConfigValues.getString("RepositoryClientServer.USER_NAME")); cfconn.setPassword(ConfigValues.getString("RepositoryClientServer.PW")); Connection conn; try {//from w w w . java 2 s. co m conn = cfconn.newConnection(); final Channel ch = conn.createChannel(); clientService = new RpcClient(ch, "", ConfigValues.getString("RepositoryClientServer.QUEUE")); } catch (final IOException ex) { throw new IllegalStateException("Client set up failed", ex); } }
From source file:org.s23m.cell.repository.client.server.RepositoryClientServer.java
License:Mozilla Public License
private void setupConnection() throws IOException { final ConnectionFactory connFactory = new ConnectionFactory(); connFactory.setHost(LOCALHOST);/*from w w w .j a v a 2s . c om*/ connFactory.setPort(PORT); connFactory.setVirtualHost(ConfigValues.getString("RepositoryClientServer.VHOST_NAME")); connFactory.setUsername(ConfigValues.getString("RepositoryClientServer.USER_NAME")); connFactory.setPassword(ConfigValues.getString("RepositoryClientServer.PW")); final Connection conn = connFactory.newConnection(); ch = conn.createChannel(); ch.queueDeclare(QUEUE_NAME, false, false, false, null); }
From source file:org.sdw.scheduler.PeriodicUpdater.java
License:Apache License
/** * Pushes messages to the shared queue on execution of triggers * @param JobExecutionContext Job execution context * @throws JobExecutionException /*from w w w. j av a2 s .c om*/ */ @Override public void execute(JobExecutionContext jobExecutionContext) { try { Connection connection = PeriodicScheduler.factory.newConnection(); Channel channel = connection.createChannel(); String queueName = "work-queue-1"; Map<String, Object> params = new HashMap<>(); params.put("x-ha-policy", "all"); channel.queueDeclare(queueName, true, false, false, params); String mesg = "Sent at: " + System.currentTimeMillis(); byte[] body = mesg.getBytes("UTF-8"); channel.basicPublish("", queueName, MessageProperties.PERSISTENT_TEXT_PLAIN, body); LOG.info("Message sent: " + mesg); connection.close(); } catch (Exception ex) { LOG.error(ex.getMessage(), ex); } }
From source file:org.sdw.scheduler.QueueProcessor.java
License:Apache License
/** * Implementation of the Runnable interface's run method * Polls for messages in the shared queue and logs the results on arrival of message *///from ww w . j a v a 2s .c o m @Override public void run() { try { ConnectionFactory factory = new ConnectionFactory(); factory.setUri(System.getenv("CLOUDAMQP_URL")); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); String queueName = "work-queue-1"; Map<String, Object> params = new HashMap<>(); params.put("x-ha-policy", "all"); channel.queueDeclare(queueName, true, false, false, params); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, false, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); if (delivery != null) { String msg = new String(delivery.getBody(), "UTF-8"); LOG.info("Message Received: " + msg); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } } catch (IOException | KeyManagementException | NoSuchAlgorithmException | URISyntaxException | ShutdownSignalException | ConsumerCancelledException | InterruptedException ex) { LOG.error(ex.getMessage(), ex); } }
From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java
License:Apache License
@Test public void testWithConnectionFactoryDefaults() throws IOException { com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock( com.rabbitmq.client.ConnectionFactory.class); com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel = mock(Channel.class); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.createChannel()).thenReturn(mockChannel); when(mockChannel.isOpen()).thenReturn(true); when(mockConnection.isOpen()).thenReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); Connection con = ccf.createConnection(); Channel channel = con.createChannel(false); channel.close(); // should be ignored, and placed into channel cache. con.close(); // should be ignored Connection con2 = ccf.createConnection(); /*/*from www .ja v a 2s . c o m*/ * will retrieve same channel object that was just put into channel cache */ Channel channel2 = con2.createChannel(false); channel2.close(); // should be ignored con2.close(); // should be ignored assertSame(con, con2); assertSame(channel, channel2); verify(mockConnection, never()).close(); verify(mockChannel, never()).close(); }
From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java
License:Apache License
@Test public void testWithConnectionFactoryCacheSize() throws IOException { com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock( com.rabbitmq.client.ConnectionFactory.class); com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel1 = mock(Channel.class); Channel mockChannel2 = mock(Channel.class); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); when(mockConnection.createChannel()).thenReturn(mockChannel1).thenReturn(mockChannel2); when(mockChannel1.basicGet("foo", false)).thenReturn(new GetResponse(null, null, null, 1)); when(mockChannel2.basicGet("bar", false)).thenReturn(new GetResponse(null, null, null, 1)); when(mockChannel1.isOpen()).thenReturn(true); when(mockChannel2.isOpen()).thenReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setChannelCacheSize(2);// w w w .j ava 2s . c o m Connection con = ccf.createConnection(); Channel channel1 = con.createChannel(false); Channel channel2 = con.createChannel(false); channel1.basicGet("foo", true); channel2.basicGet("bar", true); channel1.close(); // should be ignored, and add last into channel cache. channel2.close(); // should be ignored, and add last into channel cache. Channel ch1 = con.createChannel(false); // remove first entry in cache // (channel1) Channel ch2 = con.createChannel(false); // remove first entry in cache // (channel2) assertNotSame(ch1, ch2); assertSame(ch1, channel1); assertSame(ch2, channel2); ch1.close(); ch2.close(); verify(mockConnection, times(2)).createChannel(); con.close(); // should be ignored verify(mockConnection, never()).close(); verify(mockChannel1, never()).close(); verify(mockChannel2, never()).close(); }
From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java
License:Apache License
@Test public void testCacheSizeExceeded() throws IOException { com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock( com.rabbitmq.client.ConnectionFactory.class); com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel1 = mock(Channel.class); Channel mockChannel2 = mock(Channel.class); Channel mockChannel3 = mock(Channel.class); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.createChannel()).thenReturn(mockChannel1).thenReturn(mockChannel2) .thenReturn(mockChannel3);//from w w w . j a v a 2s . c om when(mockConnection.isOpen()).thenReturn(true); // Called during physical close when(mockChannel1.isOpen()).thenReturn(true); when(mockChannel2.isOpen()).thenReturn(true); when(mockChannel3.isOpen()).thenReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setChannelCacheSize(1); Connection con = ccf.createConnection(); Channel channel1 = con.createChannel(false); // cache size is 1, but the other connection is not released yet so this // creates a new one Channel channel2 = con.createChannel(false); assertNotSame(channel1, channel2); // should be ignored, and added last into channel cache. channel1.close(); // should be physically closed channel2.close(); // remove first entry in cache (channel1) Channel ch1 = con.createChannel(false); // create a new channel Channel ch2 = con.createChannel(false); assertNotSame(ch1, ch2); assertSame(ch1, channel1); assertNotSame(ch2, channel2); ch1.close(); ch2.close(); verify(mockConnection, times(3)).createChannel(); con.close(); // should be ignored verify(mockConnection, never()).close(); verify(mockChannel1, never()).close(); verify(mockChannel2, atLeastOnce()).close(); verify(mockChannel3, atLeastOnce()).close(); }
From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java
License:Apache License
@Test public void testCacheSizeExceededAfterClose() throws IOException { com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock( com.rabbitmq.client.ConnectionFactory.class); com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel1 = mock(Channel.class); Channel mockChannel2 = mock(Channel.class); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.createChannel()).thenReturn(mockChannel1).thenReturn(mockChannel2); when(mockConnection.isOpen()).thenReturn(true); // Called during physical close when(mockChannel1.isOpen()).thenReturn(true); when(mockChannel2.isOpen()).thenReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setChannelCacheSize(1);//ww w . jav a2s. co m Connection con = ccf.createConnection(); Channel channel1 = con.createChannel(false); channel1.close(); // should be ignored, and add last into channel cache. Channel channel2 = con.createChannel(false); channel2.close(); // should be ignored, and add last into channel cache. assertSame(channel1, channel2); Channel ch1 = con.createChannel(false); // remove first entry in cache // (channel1) Channel ch2 = con.createChannel(false); // create new channel assertNotSame(ch1, ch2); assertSame(ch1, channel1); assertNotSame(ch2, channel2); ch1.close(); ch2.close(); verify(mockConnection, times(2)).createChannel(); con.close(); // should be ignored verify(mockConnection, never()).close(); verify(mockChannel1, never()).close(); verify(mockChannel2, atLeastOnce()).close(); }