List of usage examples for com.rabbitmq.client Channel exchangeDeclare
Exchange.DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type, boolean durable) throws IOException;
From source file:org.elasticsearch.river.rabbitmq.RabbitMQTestRunner.java
License:Apache License
@Test public void test_all_messages_are_consumed() throws Exception { // We try to connect to RabbitMQ. // If it's not launched, we don't fail the test but only log it try {//from ww w. j a va 2 s . co m logger.info(" --> connecting to rabbitmq"); ConnectionFactory cfconn = new ConnectionFactory(); cfconn.setHost("localhost"); cfconn.setPort(AMQP.PROTOCOL.PORT); Connection conn = cfconn.newConnection(); Channel ch = conn.createChannel(); ch.exchangeDeclare("elasticsearch", "direct", true); AMQP.Queue.DeclareOk queue = ch.queueDeclare("elasticsearch", true, false, false, null); // We purge the queue in case of something is remaining there ch.queuePurge("elasticsearch"); logger.info(" --> sending messages"); pushMessages(ch); logger.info(" --> create river"); createIndex(INDEX); index("_river", "test", "_meta", river()); // We need at some point to check if we have consumed the river int steps = timeout(); long count = 0; while (true) { // We wait for one second Thread.sleep(1000); CountResponse response = client().prepareCount("test").execute().actionGet(); count = response.getCount(); steps--; if (steps < 0 || count == expectedDocuments()) { break; } } ch.close(); conn.close(); postInjectionTests(); } catch (ConnectException e) { throw new Exception("RabbitMQ service is not launched on localhost:" + AMQP.PROTOCOL.PORT + ". Can not start Integration test. " + "Launch `rabbitmq-server`.", e); } }
From source file:org.hp.samples.ProcessMessage.java
License:Open Source License
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain"); response.setStatus(200);//from w w w. j a v a 2s . c o m PrintWriter writer = response.getWriter(); writer.println("Here's your message:"); // Pull out the RABBITMQ_URL environment variable String uri = System.getenv("RABBITMQ_URL"); ConnectionFactory factory = new ConnectionFactory(); try { factory.setUri(uri); } catch (KeyManagementException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // Create the queue channel.queueDeclare("hello", false, false, false, null); String routingKey = "thekey"; String exchangeName = "exchange"; // Declare an exchange and bind it to the queue channel.exchangeDeclare(exchangeName, "direct", true); channel.queueBind("hello", exchangeName, routingKey); // Grab the message from the HTML form and publish it to the queue String message = request.getParameter("message"); channel.basicPublish(exchangeName, routingKey, null, message.getBytes()); writer.println(" Message sent to queue '" + message + "'"); boolean autoAck = false; // Get the response message GetResponse responseMsg = channel.basicGet("hello", autoAck); if (responseMsg == null) { // No message retrieved. } else { byte[] body = responseMsg.getBody(); // Since getBody() returns a byte array, convert to a string for // the user. String bodyString = new String(body); long deliveryTag = responseMsg.getEnvelope().getDeliveryTag(); writer.println("Message received: " + bodyString); // Acknowledge that we received the message so that the queue // removes the message so that it's not sent to us again. channel.basicAck(deliveryTag, false); } writer.close(); }
From source file:org.mobicents.servlet.sip.example.SimpleSipServlet.java
License:Open Source License
/** * {@inheritDoc}/*ww w. j a v a 2 s .c o m*/ */ protected void doNotify(SipServletRequest request) throws ServletException, IOException { Channel channel = null; String routingKey = ""; //a trick to change routingKey value. //routingKey = getBindingKey(); try { channel = pool.borrowObject(); String message = request.getCallId(); channel.exchangeDeclare(EXCHANGE_NAME, "topic", true); channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes()); logger.info("PUBLISH A MESSAGE : " + message); logger.info("*************************************"); logger.info("**" + "Number active : " + pool.getNumActive() + " ***"); logger.info("**" + "Number idle : " + pool.getNumIdle() + " ***"); logger.info("*************************************"); } catch (NoSuchElementException e) { logger.error(e.getMessage()); throw new ServletException(e); } catch (IllegalStateException e) { logger.error(e.getMessage()); throw new ServletException(e); } catch (Exception e) { logger.error(e.getMessage()); throw new ServletException(e); } finally { if (channel != null) { try { pool.returnObject(channel); } catch (Exception e) { e.printStackTrace(); logger.error("Failed to return channel back to pool. Exception message: " + e.getMessage()); } //logger.info("RETURN CHANNEL TO THE POOL"); } } SipServletResponse sipServletResponse = request.createResponse(SipServletResponse.SC_OK); sipServletResponse.send(); }
From source file:org.openmrs.module.amqpmodule.utils.impl.PublisherServiceImpl.java
License:Open Source License
@Override public boolean PublisherCreateConnection() throws java.io.IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("192.168.43.123"); factory.setPort(5672);//from w ww .j a v a2 s. co m factory.setUsername("chs"); factory.setPassword("chs123"); Connection connection = null; try { connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct", true); channel.basicPublish(EXCHANGE_NAME, topic, MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes()); System.out.println(" [x] Sent '" + msg + "'"); channel.close(); } catch (TimeoutException e) { System.out.println("Connection Timed out"); e.printStackTrace(); } connection.close(); return true; }
From source file:org.smartdeveloperhub.harvesters.it.notification.CollectorControllerTest.java
License:Apache License
@Test public void testConnect$shouldConnectIfExchangeAlreadyAvailable(@Mocked final Channel channel) throws Exception { new MockUp<ConnectionManager>() { private boolean connected = false; @Mock(invocations = 1)/* www . j av a 2s .c o m*/ void connect() { this.connected = true; } @Mock(invocations = 1) void disconnect() { } @Mock boolean isConnected() { return this.connected; } @Mock Channel channel() { return channel; } }; new MockUp<FailureAnalyzer>() { @Mock boolean isExchangeDeclarationRecoverable(final IOException e) { return true; } }; final CollectorConfiguration defaultCollector = defaultCollector(); new Expectations() { { channel.exchangeDeclare(defaultCollector.getExchangeName(), "topic", true); this.result = new IOException("Failure"); } }; final CollectorController sut = collectorController(defaultCollector); try { sut.connect(); } finally { sut.disconnect(); } }
From source file:org.smartdeveloperhub.harvesters.it.notification.CollectorControllerTest.java
License:Apache License
@Test public void testConnect$shouldCleanUpOnExchangeSetupFailure(@Mocked final Channel channel) throws Exception { new MockUp<ConnectionManager>() { private boolean connected = false; @Mock(invocations = 1)/* w ww. j av a 2 s. c om*/ void connect() { this.connected = true; } @Mock(invocations = 1) void disconnect() { } @Mock boolean isConnected() { return this.connected; } @Mock Channel channel() { return channel; } }; final CollectorConfiguration defaultCollector = defaultCollector(); new Expectations() { { channel.exchangeDeclare(defaultCollector.getExchangeName(), "topic", true); this.result = new IOException("Failure"); } }; final CollectorController sut = collectorController(defaultCollector); try { sut.connect(); fail("Should not connect on failure"); } catch (final ControllerException e) { assertThat(e.getMessage(), equalTo("Could not create exchange named '" + defaultCollector.getExchangeName() + "'")); assertThat(e.getCause(), instanceOf(IOException.class)); assertThat(e.getCause().getMessage(), equalTo("Failure")); } }
From source file:org.smartdeveloperhub.harvesters.scm.backend.notification.CollectorControllerTest.java
License:Apache License
@Test public void testConnect$shouldConnectIfExchangeAlreadyAvailable(@Mocked final Channel channel) throws Exception { new MockUp<ConnectionManager>() { private boolean connected = false; @Mock(invocations = 1)// w w w. j a v a 2 s . co m void connect() { this.connected = true; } @Mock(invocations = 1) void disconnect() { } @Mock boolean isConnected() { return this.connected; } @Mock Channel channel() { return channel; } }; new MockUp<FailureAnalyzer>() { @Mock boolean isExchangeDeclarationRecoverable(final IOException e) { return true; } }; final Collector defaultCollector = defaultCollector(); new Expectations() { { channel.exchangeDeclare(defaultCollector.getExchangeName(), "topic", true); this.result = new IOException("Failure"); } }; final CollectorController sut = collectorController(defaultCollector); try { sut.connect(); } finally { sut.disconnect(); } }
From source file:org.smartdeveloperhub.harvesters.scm.backend.notification.CollectorControllerTest.java
License:Apache License
@Test public void testConnect$shouldCleanUpOnExchangeSetupFailure(@Mocked final Channel channel) throws Exception { new MockUp<ConnectionManager>() { private boolean connected = false; @Mock(invocations = 1)/*from w w w.j ava2 s. c o m*/ void connect() { this.connected = true; } @Mock(invocations = 1) void disconnect() { } @Mock boolean isConnected() { return this.connected; } @Mock Channel channel() { return channel; } }; final Collector defaultCollector = defaultCollector(); new Expectations() { { channel.exchangeDeclare(defaultCollector.getExchangeName(), "topic", true); this.result = new IOException("Failure"); } }; final CollectorController sut = collectorController(defaultCollector); try { sut.connect(); fail("Should not connect on failure"); } catch (final ControllerException e) { assertThat(e.getMessage(), equalTo("Could not create exchange named '" + defaultCollector.getExchangeName() + "'")); assertThat(e.getCause(), instanceOf(IOException.class)); assertThat(e.getCause().getMessage(), equalTo("Failure")); } }
From source file:org.springframework.integration.amqp.config.EchoSample.java
License:Apache License
@BeforeClass public static void setup() { SingleConnectionFactory connectionFactory = new SingleConnectionFactory(); connectionFactory.setUsername("guest"); connectionFactory.setPassword("guest"); RabbitTemplate template = new RabbitTemplate(connectionFactory); template.execute(new ChannelCallback<Object>() { @Override/*from w ww .jav a 2s. c om*/ public Object doInRabbit(Channel channel) throws Exception { channel.queueDeclare("si.test.queue", true, false, false, null); channel.exchangeDeclare("si.test.exchange", "direct", true); channel.queueBind("si.test.queue", "si.test.exchange", "si.test.binding"); return null; } }); }
From source file:org.teksme.server.common.messaging.AMQPBrokerManager.java
License:Apache License
protected void declareQueueing(Connection conn, MessageMiddleware config) throws IOException { String routingKey = null;//from w ww.ja va 2 s. com String exchangeName = null; String queueName = null; String type = null; boolean durable = false; boolean autoDelete = false; boolean exclusive = false; List<MessageMiddleware.Queue> queues = config.getQueues(); for (Queue queue : queues) { queueName = queue.getName(); routingKey = queue.getKey(); exchangeName = queue.getExchange(); type = queue.getType(); durable = queue.isDurable(); autoDelete = queue.isAutoDelete(); exclusive = queue.isExclusive(); logger.info("Declaring exchange [" + exchangeName + "] and queue [" + queueName + "]"); Channel channel = conn.createChannel(); channel.exchangeDeclare(exchangeName, type, durable); channel.queueDeclare(queueName, durable, exclusive, autoDelete, null); logger.info("Binding queue [" + queueName + "] and exchange [" + exchangeName + "] to routing key [" + routingKey + "]"); channel.queueBind(queueName, exchangeName, routingKey); } }