1.在以下配置下,立即收到调度程序消息。
2.如果删除@Profile("embedded")
的注释brokerService()
,则会引发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;
}
}