Example usage for org.springframework.amqp.rabbit.connection Connection createChannel

List of usage examples for org.springframework.amqp.rabbit.connection Connection createChannel

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.connection Connection createChannel.

Prototype

Channel createChannel(boolean transactional) throws AmqpException;

Source Link

Document

Create a new channel, using an internally allocated channel number.

Usage

From source file:org.springframework.integration.amqp.config.AmqpOutboundChannelAdapterParserTests.java

@Test
public void testInt2773UseDefaultAmqpTemplateExchangeAndRoutingLey() throws IOException {
    ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
    Connection mockConnection = mock(Connection.class);
    Channel mockChannel = mock(Channel.class);

    when(connectionFactory.createConnection()).thenReturn(mockConnection);
    PublisherCallbackChannelImpl publisherCallbackChannel = new PublisherCallbackChannelImpl(mockChannel);
    when(mockConnection.createChannel(false)).thenReturn(publisherCallbackChannel);

    MessageChannel requestChannel = context.getBean("toRabbitOnlyWithTemplateChannel", MessageChannel.class);
    requestChannel.send(MessageBuilder.withPayload("test").build());
    Mockito.verify(mockChannel, Mockito.times(1)).basicPublish(Mockito.eq("default.test.exchange"),
            Mockito.eq("default.routing.key"), Mockito.anyBoolean(), Mockito.any(BasicProperties.class),
            Mockito.any(byte[].class));
}

From source file:org.springframework.integration.amqp.config.AmqpOutboundChannelAdapterParserTests.java

@Test
public void testInt2773WithDefaultAmqpTemplateExchangeAndRoutingKey() throws IOException {
    ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
    Connection mockConnection = mock(Connection.class);
    Channel mockChannel = mock(Channel.class);

    when(connectionFactory.createConnection()).thenReturn(mockConnection);
    PublisherCallbackChannelImpl publisherCallbackChannel = new PublisherCallbackChannelImpl(mockChannel);
    when(mockConnection.createChannel(false)).thenReturn(publisherCallbackChannel);

    MessageChannel requestChannel = context.getBean("withDefaultAmqpTemplateExchangeAndRoutingKey",
            MessageChannel.class);
    requestChannel.send(MessageBuilder.withPayload("test").build());
    Mockito.verify(mockChannel, Mockito.times(1)).basicPublish(Mockito.eq(""), Mockito.eq(""),
            Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class));
}

From source file:org.springframework.integration.amqp.config.AmqpOutboundChannelAdapterParserTests.java

@Test
public void testInt2773WithOverrideToDefaultAmqpTemplateExchangeAndRoutingLey() throws IOException {
    ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
    Connection mockConnection = mock(Connection.class);
    Channel mockChannel = mock(Channel.class);

    when(connectionFactory.createConnection()).thenReturn(mockConnection);
    PublisherCallbackChannelImpl publisherCallbackChannel = new PublisherCallbackChannelImpl(mockChannel);
    when(mockConnection.createChannel(false)).thenReturn(publisherCallbackChannel);

    MessageChannel requestChannel = context.getBean("overrideTemplateAttributesToEmpty", MessageChannel.class);
    requestChannel.send(MessageBuilder.withPayload("test").build());
    Mockito.verify(mockChannel, Mockito.times(1)).basicPublish(Mockito.eq(""), Mockito.eq(""),
            Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class));
}

From source file:org.springframework.integration.amqp.inbound.AmqpMessageSource.java

@Override
protected AbstractIntegrationMessageBuilder<Object> doReceive() {
    Connection connection = this.connectionFactory.createConnection(); // NOSONAR - RabbitUtils
    Channel channel = connection.createChannel(this.transacted);
    try {//from www .ja v  a2s. c  om
        GetResponse resp = channel.basicGet(this.queue, false);
        if (resp == null) {
            RabbitUtils.closeChannel(channel);
            RabbitUtils.closeConnection(connection);
            return null;
        }
        AcknowledgmentCallback callback = this.ackCallbackFactory
                .createCallback(new AmqpAckInfo(connection, channel, this.transacted, resp));
        MessageProperties messageProperties = this.propertiesConverter.toMessageProperties(resp.getProps(),
                resp.getEnvelope(), StandardCharsets.UTF_8.name());
        messageProperties.setConsumerQueue(this.queue);
        Map<String, Object> headers = this.headerMapper.toHeadersFromRequest(messageProperties);
        org.springframework.amqp.core.Message amqpMessage = new org.springframework.amqp.core.Message(
                resp.getBody(), messageProperties);
        Object payload = this.messageConverter.fromMessage(amqpMessage);
        AbstractIntegrationMessageBuilder<Object> builder = getMessageBuilderFactory().withPayload(payload)
                .copyHeaders(headers)
                .setHeader(IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK, callback);
        if (this.rawMessageHeader) {
            builder.setHeader(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE, amqpMessage);
        }
        return builder;
    } catch (IOException e) {
        RabbitUtils.closeChannel(channel);
        RabbitUtils.closeConnection(connection);
        throw RabbitExceptionTranslator.convertRabbitAccessException(e);
    }
}