2 min read

Java Snippets - Spring JMS

Mallim

Possible ways I found out to setup Spring JMS. If the default, cannot satisfy you...

Container Factory

DefaultJmsListenerContainerFactoryConfigurer

Good comment in the code about DefaultJmsListenerContainerFactoryConfigurer ...

@Bean
public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
                                                DefaultJmsListenerContainerFactoryConfigurer configurer) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    // This provides all boot's default to this factory, including the message converter
    configurer.configure(factory, connectionFactory);
    // You could still override some of Boot's default if necessary.
    return factory;
}

https://github.com/spring-guides/gs-messaging-jms/tree/master/complete

JmsListenerContainerFactory<?>

@Bean
public JmsListenerContainerFactory<?> myFactory(
    ConnectionFactory connectionFactory,
    DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

// anonymous class
factory.setErrorHandler(
    new ErrorHandler() {
        @Override
        public void handleError(Throwable t) {
        System.err.println("An error has occurred in the transaction");
        }
    });

// OR lambda function
factory.setErrorHandler(t -> System.out.println("An error has occurred in the transaction"));

configurer.configure(factory, connectionFactory);
return factory;
}

https://github.com/lankydan/spring-boot-jms

Connection Factories

PooledConnectionFactory

ConnectionFactory connectionFactory() {
    ConnectionFactory connectionFactory = new PooledConnectionFactory("tcp://localhost:61616");
    return connectionFactory;
}

@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory());
    factory.setDestinationResolver(new DynamicDestinationResolver());
    factory.setMessageConverter(MessageConverters.defaultMessageConverter());
    factory.setConcurrency("3-10");
    return factory;
}

https://github.com/pjayes/spring-boot-jms-example

CachingConnectionFactory

  @Bean
  public CachingConnectionFactory cachingConnectionFactory() {
    CachingConnectionFactory cachingConnectionFactory =
        new CachingConnectionFactory(senderConnectionFactory());
    cachingConnectionFactory.setSessionCacheSize(10);

    return cachingConnectionFactory;
  }

@Bean
public JmsTemplate jmsTemplate() {
  return new JmsTemplate(cachingConnectionFactory());
}

Reference:

The Others

JmsListener

destination:
  order: order.q
  status1: status1.q
  status2: status2.q
  @Bean
  public DefaultJmsListenerContainerFactory orderDefaultJmsListenerContainerFactory() {
    DefaultJmsListenerContainerFactory factory =
        new DefaultJmsListenerContainerFactory();
    factory
        .setConnectionFactory(receiverActiveMQConnectionFactory());
    factory.setConcurrency("3-10");

    return factory;
  }

 @JmsListener(destination = "${destination.order}",
      containerFactory = "orderDefaultJmsListenerContainerFactory")

https://github.com/code-not-found/spring-jms/tree/master/spring-jms-listener

javax.jms.Destination

  @Bean
  public Destination orderDestination() {
    return new ActiveMQQueue(orderDestination);
  }

  @Bean
  public JmsTemplate orderJmsTemplate() {
    JmsTemplate jmsTemplate =
        new JmsTemplate(cachingConnectionFactory());
    jmsTemplate.setDefaultDestination(orderDestination());
    jmsTemplate.setReceiveTimeout(5000);

    return jmsTemplate;
  }

https://github.com/code-not-found/spring-jms/blob/master/spring-jms-jmstemplate

JmsTemplate #2

// Example configuration of JmsTemplate
@Bean
public JmsTemplate jmsTemplate( ConnectionFactory connectionFactory ) {
    CachingConnectionFactory ccf = new CachingConnectionFactory(connectionFactory);
    JmsTemplate jmst = new JmsTemplate(ccf);
    return jmst;
}

https://solace.com/samples/solace-samples-cloudfoundry-java/spring-cloud-autoconf-jms/