ActiveMQ scheduler message in spring

2019-08-03 19:38发布

问题:

1.under below configuration, scheduler messages are received immediately.

2.if removing the annotation@Profile("embedded") of brokerService(),it will throw a java.net.BindException: Address already in use: JVM_Bind exception.

@Configuration
@EnableJms
public class MessageConfig {
    private static final String DEFAULT_BROKER_URL = "tcp://localhost:61616";

    public static final String DESTINATION_FB = "fb";
    private static final String USER_NAME = "admin";
    private static final String USER_PASSWORD = "admin";

    @Profile("embedded")
    @Bean(initMethod = "start", destroyMethod = "stop")
    public BrokerService brokerService() throws Exception {
        BrokerService brokerService = new BrokerService();
        brokerService.setSchedulerSupport(true);
        brokerService.addConnector(DEFAULT_BROKER_URL);
        return brokerService;
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(DEFAULT_BROKER_URL);
         /*  connectionFactory.setUserName(USER_NAME);
        connectionFactory.setPassword(USER_PASSWORD);*/
        connectionFactory.setTrustedPackages(Arrays.asList("com.test.test", "java.lang"));
        return connectionFactory;
    }

    @Bean
    public JmsTemplate jmsTemplate(ConnectionFactory factory) {
        JmsTemplate template = new JmsTemplate();
        template.setConnectionFactory(factory);
        template.setDefaultDestinationName(DESTINATION_FB);
        return template;
    }

    @Bean
    public DefaultMessageListenerContainer jmsListenerContainerFactory(ConnectionFactory connectionFactory) {
        DefaultMessageListenerContainer containerFactory = new DefaultMessageListenerContainer();
        containerFactory.setConnectionFactory(connectionFactory);
        return containerFactory;
    }
}