Search in sources :

Example 1 with AmqpTemplate

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();
}
Also used : AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) AmqpTemplate(org.springframework.amqp.core.AmqpTemplate)

Example 2 with AmqpTemplate

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();
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) RabbitAdmin(org.springframework.amqp.rabbit.core.RabbitAdmin) Queue(org.springframework.amqp.core.Queue) AmqpTemplate(org.springframework.amqp.core.AmqpTemplate)

Example 3 with AmqpTemplate

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);
}
Also used : ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) Message(org.springframework.amqp.core.Message) Channel(com.rabbitmq.client.Channel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageListener(org.springframework.amqp.core.MessageListener) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AmqpTemplate(org.springframework.amqp.core.AmqpTemplate) Test(org.junit.Test)

Example 4 with AmqpTemplate

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'."));
    }
}
Also used : DeclareOk(com.rabbitmq.client.AMQP.Queue.DeclareOk) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) Message(org.springframework.amqp.core.Message) Channel(com.rabbitmq.client.Channel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageListener(org.springframework.amqp.core.MessageListener) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) AmqpTemplate(org.springframework.amqp.core.AmqpTemplate) Test(org.junit.Test)

Example 5 with AmqpTemplate

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");
}
Also used : SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) BeanFactory(org.springframework.beans.factory.BeanFactory) RabbitAdmin(org.springframework.amqp.rabbit.core.RabbitAdmin) Queue(org.springframework.amqp.core.Queue) AmqpTemplate(org.springframework.amqp.core.AmqpTemplate) Test(org.junit.Test)

Aggregations

AmqpTemplate (org.springframework.amqp.core.AmqpTemplate)5 Test (org.junit.Test)3 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)3 SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)3 BeanFactory (org.springframework.beans.factory.BeanFactory)3 Channel (com.rabbitmq.client.Channel)2 Message (org.springframework.amqp.core.Message)2 MessageListener (org.springframework.amqp.core.MessageListener)2 Queue (org.springframework.amqp.core.Queue)2 Connection (org.springframework.amqp.rabbit.connection.Connection)2 RabbitAdmin (org.springframework.amqp.rabbit.core.RabbitAdmin)2 DeclareOk (com.rabbitmq.client.AMQP.Queue.DeclareOk)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)1 AbstractApplicationContext (org.springframework.context.support.AbstractApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)1