Spring-Topic

생산자
1. pom 파일

    org.springframework.boot
    spring-boot-starter-activemq


2. yml 파일
# springboot     
server:
  port: 8083
# activeMQ  
spring:
  activemq:
    broker-url: tcp://112.124.20.231:61616 # activeMQ   ip
    user: admin
    password: admin
  #           
  jms:
    pub-sub-domain: true # false = Queue | true = Topic
#         
myTopicName: springboot-activemq-topic

3. bean 설정
@Component
@EnableJms
public class ConfigBean {
    @Value("${myTopicName}")
    private String topicName;

    @Bean
    public ActiveMQTopic activeMQTopic(){
        return new ActiveMQTopic(topicName);
    }
}

4. Topic_Producer
@Component
public class Topic_Producer {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private ActiveMQTopic activeMQTopic;

    @Scheduled(fixedDelay = 3000)
    public void producer(){
        jmsMessagingTemplate.convertAndSend(activeMQTopic,"    :"+ UUID.randomUUID().toString());
    }
}

5. 시작 클래스
@SpringBootApplication
@EnableScheduling
public class BootTopicProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootTopicProducerApplication.class, args);
    }

}

소비자
pom.xml

    org.springframework.boot
    spring-boot-starter-activemq


yml 파일
# springboot     
server:
  port: 8084
# activeMQ  
spring:
  activemq:
    broker-url: tcp://112.124.20.231:61616 # activeMQ   ip
    user: admin
    password: admin
  #           
  jms:
    pub-sub-domain: true # false = Queue | true = Topic
#         
myTopicName: springboot-activemq-topic

Topic_Consumer
@Component
public class Topic_Consumer {
    @JmsListener(destination = "${myTopicName}")
    public void consumer(TextMessage textMessage) throws Exception{
        System.out.println("        :"+textMessage.getText());
    }
}

시동 을 걸다
@SpringBootApplication
public class BootTopicConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootTopicConsumerApplication.class, args);
    }
}

영구 판
configBean 설정
@Component
@EnableJms
public class ConfigBean {
    @Value("${spring.activemq.broker-url}")
    private String brokerUrl;
    @Value("${spring.activemq.user}")
    private String user;
    @Value("${spring.activemq.password}")
    private String password;

    public ConnectionFactory connectionFactory(){
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(brokerUrl);
        connectionFactory.setUserName(user);
        connectionFactory.setPassword(password);
        return connectionFactory;
    }

    @Bean(name = "jmsListenerContainerFactory")
    public DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory(){
        DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
        defaultJmsListenerContainerFactory.setConnectionFactory(connectionFactory());
        defaultJmsListenerContainerFactory.setSubscriptionDurable(true);
        defaultJmsListenerContainerFactory.setClientId("         ");
        return defaultJmsListenerContainerFactory;
    }

}

Topic_Consumer
@Component
public class Topic_Consumer {
    @JmsListener(destination = "${myTopicName}",containerFactory = "jmsListenerContainerFactory")
    public void consumer(TextMessage textMessage) throws Exception{
        System.out.println("        :"+textMessage.getText());
    }

}

좋은 웹페이지 즐겨찾기