Skip to content

Commit

Permalink
Using a topic instead of a queue
Browse files Browse the repository at this point in the history
  • Loading branch information
rladdusaw committed Apr 24, 2019
1 parent d8f422f commit 6e1b0a4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@

@AliasFor(annotation = JmsListener.class, attribute = "destination")
String destination() default "default";

@AliasFor(annotation = JmsListener.class, attribute = "containerFactory")
String containerFactory() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package edu.tamu.weaver.messaging.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;

@Configuration
public class MessagingConfig {

@Value("${spring.activemq.broker-url}")
private String brokerUrl;

@Bean
public CachingConnectionFactory cachingConnectionFactory() {
return new CachingConnectionFactory(defaultActiveMQConnectionFactory());
}

@Bean
public JmsTemplate jmsTopicTemplate() {
JmsTemplate jmsTemplate = new JmsTemplate(cachingConnectionFactory());
jmsTemplate.setPubSubDomain(true);
return jmsTemplate;
}

public ActiveMQConnectionFactory defaultActiveMQConnectionFactory() {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
activeMQConnectionFactory.setBrokerURL(brokerUrl);
return activeMQConnectionFactory;
}

@Bean
public JmsListenerContainerFactory<?> topicContainerFactory() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(defaultActiveMQConnectionFactory());
factory.setPubSubDomain(true);
return factory;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
Expand All @@ -14,8 +15,8 @@ public class MessagingService {
private final JmsMessagingTemplate jmsTemplate;

@Autowired
public MessagingService(JmsMessagingTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
public MessagingService(JmsTemplate jmsTopicTemplate) {
this.jmsTemplate = new JmsMessagingTemplate(jmsTopicTemplate);
}

public void sendMessage(String destination, Map<String, String> payload) {
Expand Down

0 comments on commit 6e1b0a4

Please sign in to comment.