Search in sources :

Example 16 with Envelope

use of com.rabbitmq.client.Envelope in project uavstack by uavorg.

the class TestRestService method testRabbitmq.

@GET
@Path("testRabbitmq")
public void testRabbitmq(String jsonString) {
    String QUEUE_NAME = "haha";
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername("guest");
    factory.setPassword("guest");
    factory.setHost("127.0.0.1");
    factory.setPort(5672);
    try {
        com.rabbitmq.client.Connection conn = factory.newConnection();
        Channel channel = conn.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();
        conn.close();
        com.rabbitmq.client.Connection connection = factory.newConnection();
        Channel recvchannel = connection.createChannel();
        recvchannel.queueDeclare(QUEUE_NAME, false, false, false, null);
        com.rabbitmq.client.Consumer consumer = new DefaultConsumer(recvchannel) {

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
            // System.out.println(" [x] Received '" + message + "'1");
            }
        };
        recvchannel.basicConsume(QUEUE_NAME, true, consumer);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (TimeoutException e) {
        e.printStackTrace();
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Channel(com.rabbitmq.client.Channel) IOException(java.io.IOException) Envelope(com.rabbitmq.client.Envelope) TimeoutException(java.util.concurrent.TimeoutException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 17 with Envelope

use of com.rabbitmq.client.Envelope in project uavstack by uavorg.

the class RabbitMQRecv method send.

@POST
@Path("rabbitmqRecv")
public void send() throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername("guest");
    factory.setPassword("guest");
    factory.setHost("127.0.0.1");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    DefaultConsumer consumer = new DefaultConsumer(channel) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            System.out.println(properties.getHeaders());
            String message = new String(body, "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
        }
    };
    channel.basicConsume(QUEUE_NAME, true, consumer);
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) Envelope(com.rabbitmq.client.Envelope) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 18 with Envelope

use of com.rabbitmq.client.Envelope in project rabbitmq-queue-management by gessnerfl.

the class MessageMapperTest method shouldMapRabbitMqSpecificTypesInAmqpBasicPropertiesHeadersDeepInMap.

@Test
public void shouldMapRabbitMqSpecificTypesInAmqpBasicPropertiesHeadersDeepInMap() {
    String headerKey = "headerKey";
    String subHeaderKey = "subHeaderKey";
    String subHeaderValue = "subHeaderValue";
    LongString longStringValue = mock(LongString.class);
    when(longStringValue.toString()).thenReturn(subHeaderValue);
    Map<String, Object> subHeaders = new HashMap<>();
    subHeaders.put(subHeaderKey, longStringValue);
    Map<String, Object> headers = new HashMap<>();
    headers.put(headerKey, subHeaders);
    AMQP.BasicProperties basicProperties = mock(AMQP.BasicProperties.class);
    when(basicProperties.getHeaders()).thenReturn(headers);
    Envelope envelope = mock(Envelope.class);
    GetResponse getResponse = mockResponse(envelope, basicProperties);
    Message result = sut.map(getResponse);
    BasicProperties basicPropertiesOfResult = result.getProperties();
    assertNotNull(basicPropertiesOfResult);
    assertThat(basicPropertiesOfResult.getHeaders().keySet(), Matchers.hasSize(1));
    assertThat(basicPropertiesOfResult.getHeaders(), Matchers.hasKey(headerKey));
    assertThat(basicPropertiesOfResult.getHeaders().get(headerKey), Matchers.instanceOf(Map.class));
    assertThat(((Map<String, Object>) basicPropertiesOfResult.getHeaders().get(headerKey)).keySet(), Matchers.hasSize(1));
    assertThat(((Map<String, Object>) basicPropertiesOfResult.getHeaders().get(headerKey)), Matchers.hasKey(subHeaderKey));
    assertEquals(subHeaderValue, ((Map) basicPropertiesOfResult.getHeaders().get(headerKey)).get(subHeaderKey));
}
Also used : Message(de.gessnerfl.rabbitmq.queue.management.model.Message) AMQP(com.rabbitmq.client.AMQP) BasicProperties(de.gessnerfl.rabbitmq.queue.management.model.BasicProperties) LongString(com.rabbitmq.client.LongString) LongString(com.rabbitmq.client.LongString) Envelope(com.rabbitmq.client.Envelope) GetResponse(com.rabbitmq.client.GetResponse) Test(org.junit.Test)

Example 19 with Envelope

use of com.rabbitmq.client.Envelope in project rabbitmq-queue-management by gessnerfl.

the class MessageMapperTest method shouldMapRabbitMqSpecificTypesInAmqpBasicPropertiesHeaders.

@Test
public void shouldMapRabbitMqSpecificTypesInAmqpBasicPropertiesHeaders() {
    String headerKey = "headerKey";
    String headerValue = "headerValue";
    LongString longStringValue = mock(LongString.class);
    when(longStringValue.toString()).thenReturn(headerValue);
    Map<String, Object> headers = new HashMap<>();
    headers.put(headerKey, longStringValue);
    AMQP.BasicProperties basicProperties = mock(AMQP.BasicProperties.class);
    when(basicProperties.getHeaders()).thenReturn(headers);
    Envelope envelope = mock(Envelope.class);
    GetResponse getResponse = mockResponse(envelope, basicProperties);
    Message result = sut.map(getResponse);
    BasicProperties basicPropertiesOfResult = result.getProperties();
    assertNotNull(basicPropertiesOfResult);
    assertThat(basicPropertiesOfResult.getHeaders().keySet(), Matchers.hasSize(1));
    assertThat(basicPropertiesOfResult.getHeaders(), Matchers.hasKey(headerKey));
    assertEquals(headerValue, basicPropertiesOfResult.getHeaders().get(headerKey));
}
Also used : Message(de.gessnerfl.rabbitmq.queue.management.model.Message) AMQP(com.rabbitmq.client.AMQP) BasicProperties(de.gessnerfl.rabbitmq.queue.management.model.BasicProperties) LongString(com.rabbitmq.client.LongString) LongString(com.rabbitmq.client.LongString) Envelope(com.rabbitmq.client.Envelope) GetResponse(com.rabbitmq.client.GetResponse) Test(org.junit.Test)

Example 20 with Envelope

use of com.rabbitmq.client.Envelope in project rabbitmq-queue-management by gessnerfl.

the class MessageMapperTest method shouldMapBasicMessageFields.

@Test
public void shouldMapBasicMessageFields() {
    String checksum = "checksum";
    Envelope envelope = mock(Envelope.class);
    AMQP.BasicProperties basicProperties = mock(AMQP.BasicProperties.class);
    GetResponse getResponse = mockResponse(envelope, basicProperties);
    when(messageChecksum.createFor(basicProperties, DEFAULT_PAYLOAD)).thenReturn(checksum);
    Message result = sut.map(getResponse);
    assertEquals(DEFAULT_PAYLOAD, result.getBody());
    assertEquals(checksum, result.getChecksum());
    assertSame(envelope, result.getEnvelope());
    assertNotNull(result.getProperties());
}
Also used : Message(de.gessnerfl.rabbitmq.queue.management.model.Message) AMQP(com.rabbitmq.client.AMQP) LongString(com.rabbitmq.client.LongString) Envelope(com.rabbitmq.client.Envelope) GetResponse(com.rabbitmq.client.GetResponse) Test(org.junit.Test)

Aggregations

Envelope (com.rabbitmq.client.Envelope)31 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)20 AMQP (com.rabbitmq.client.AMQP)18 IOException (java.io.IOException)17 Channel (com.rabbitmq.client.Channel)16 Test (org.junit.Test)12 Connection (com.rabbitmq.client.Connection)11 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)10 TimeoutException (java.util.concurrent.TimeoutException)9 GetResponse (com.rabbitmq.client.GetResponse)8 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)6 LongString (com.rabbitmq.client.LongString)5 Consumer (com.rabbitmq.client.Consumer)4 BasicProperties (de.gessnerfl.rabbitmq.queue.management.model.BasicProperties)4 Message (de.gessnerfl.rabbitmq.queue.management.model.Message)4 KeyManagementException (java.security.KeyManagementException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 UUID (java.util.UUID)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Path (javax.ws.rs.Path)3