use of org.springframework.amqp.rabbit.connection.Connection in project spring-integration by spring-projects.
the class AmqpOutboundChannelAdapterParserTests method testInt2773WithOverrideToDefaultAmqpTemplateExchangeAndRoutingLey.
@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));
}
use of org.springframework.amqp.rabbit.connection.Connection in project spring-integration by spring-projects.
the class AmqpMessageSource method doReceive.
@Override
protected AbstractIntegrationMessageBuilder<Object> doReceive() {
Connection connection = this.connectionFactory.createConnection();
Channel channel = connection.createChannel(this.transacted);
try {
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);
}
}
use of org.springframework.amqp.rabbit.connection.Connection in project spring-integration by spring-projects.
the class AbstractAmqpOutboundEndpoint method start.
@Override
public synchronized void start() {
if (!this.running) {
if (!this.lazyConnect && this.connectionFactory != null) {
try {
// NOSONAR (close)
Connection connection = this.connectionFactory.createConnection();
if (connection != null) {
connection.close();
}
} catch (RuntimeException e) {
logger.error("Failed to eagerly establish the connection.", e);
}
}
doStart();
this.running = true;
}
}
use of org.springframework.amqp.rabbit.connection.Connection in project spring-integration by spring-projects.
the class DispatcherHasNoSubscribersTests method testPubSub.
@Test
public void testPubSub() {
final Channel channel = mock(Channel.class);
Connection connection = mock(Connection.class);
doAnswer(invocation -> channel).when(connection).createChannel(anyBoolean());
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
when(connectionFactory.createConnection()).thenReturn(connection);
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
AmqpTemplate amqpTemplate = mock(AmqpTemplate.class);
PublishSubscribeAmqpChannel amqpChannel = new PublishSubscribeAmqpChannel("noSubscribersChannel", container, amqpTemplate);
amqpChannel.setBeanName("noSubscribersChannel");
amqpChannel.setBeanFactory(mock(BeanFactory.class));
amqpChannel.afterPropertiesSet();
List<String> logList = insertMockLoggerInListener(amqpChannel);
MessageListener listener = (MessageListener) container.getMessageListener();
listener.onMessage(new Message("Hello world!".getBytes(), null));
verifyLogReceived(logList);
}
use of org.springframework.amqp.rabbit.connection.Connection in project spring-integration by spring-projects.
the class DispatcherHasNoSubscribersTests method testPtP.
@SuppressWarnings("unchecked")
@Test
public void testPtP() throws Exception {
final Channel channel = mock(Channel.class);
DeclareOk declareOk = mock(DeclareOk.class);
when(declareOk.getQueue()).thenReturn("noSubscribersChannel");
when(channel.queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), isNull())).thenReturn(declareOk);
Connection connection = mock(Connection.class);
doAnswer(invocation -> channel).when(connection).createChannel(anyBoolean());
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
when(connectionFactory.createConnection()).thenReturn(connection);
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
AmqpTemplate amqpTemplate = mock(AmqpTemplate.class);
PointToPointSubscribableAmqpChannel amqpChannel = new PointToPointSubscribableAmqpChannel("noSubscribersChannel", container, amqpTemplate);
amqpChannel.setBeanName("noSubscribersChannel");
amqpChannel.setBeanFactory(mock(BeanFactory.class));
amqpChannel.afterPropertiesSet();
MessageListener listener = (MessageListener) container.getMessageListener();
try {
listener.onMessage(new Message("Hello world!".getBytes(), null));
fail("Exception expected");
} catch (MessageDeliveryException e) {
assertThat(e.getMessage(), containsString("Dispatcher has no subscribers for amqp-channel 'noSubscribersChannel'."));
}
}
Aggregations