use of org.springframework.amqp.core.AmqpTemplate in project tutorials by eugenp.
the class Producer method main.
public static void main(String[] args) throws InterruptedException {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
AmqpTemplate template = ctx.getBean(RabbitTemplate.class);
template.convertAndSend("Hello, world!");
Thread.sleep(1000);
ctx.destroy();
}
use of org.springframework.amqp.core.AmqpTemplate in project spring-integration by spring-projects.
the class PollableAmqpChannel method onInit.
@Override
protected void onInit() throws Exception {
AmqpTemplate amqpTemplate = getAmqpTemplate();
if (this.queue == null) {
if (getAdmin() == null && amqpTemplate instanceof RabbitTemplate) {
ConnectionFactory connectionFactory = ((RabbitTemplate) amqpTemplate).getConnectionFactory();
setAdmin(new RabbitAdmin(connectionFactory));
setConnectionFactory(connectionFactory);
}
Assert.notNull(getAdmin(), "If no queueName is configured explicitly, an AmqpAdmin instance must be provided, " + "or the AmqpTemplate must be a RabbitTemplate since the Queue needs to be declared.");
this.queue = new Queue(this.channelName);
}
super.onInit();
}
use of org.springframework.amqp.core.AmqpTemplate 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.core.AmqpTemplate 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'."));
}
}
use of org.springframework.amqp.core.AmqpTemplate in project spring-integration by spring-projects.
the class ChannelTests method channelDeclarationTests.
/*
* Verify queue is declared if not present and not declared if it is already present.
*/
@Test
public void channelDeclarationTests() {
RabbitAdmin admin = new RabbitAdmin(this.connectionFactory);
admin.deleteQueue("implicit");
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(this.connectionFactory);
container.setAutoStartup(false);
AmqpTemplate amqpTemplate = mock(AmqpTemplate.class);
PointToPointSubscribableAmqpChannel channel = new PointToPointSubscribableAmqpChannel("implicit", container, amqpTemplate);
channel.setBeanFactory(mock(BeanFactory.class));
channel.afterPropertiesSet();
channel.onCreate(null);
assertNotNull(admin.getQueueProperties("implicit"));
admin.deleteQueue("explicit");
channel.setQueueName("explicit");
channel.afterPropertiesSet();
channel.onCreate(null);
assertNotNull(admin.getQueueProperties("explicit"));
admin.deleteQueue("explicit");
// verify no declaration if exists with non-standard props
admin.declareQueue(new Queue("explicit", false));
channel.afterPropertiesSet();
channel.onCreate(null);
assertNotNull(admin.getQueueProperties("explicit"));
admin.deleteQueue("explicit");
}
Aggregations