我正在尝试使用Spring 4和ActiveMQ实现一个非XML JMS监听器。 我的问题是我的客户端一直报以下错误:
Setup of JMS message listener invoker failed for destination 'topic.FromJndiProperties' [...]
Cause: The JMS connection has failed: Force close due to SecurityException on connect.
Cause: User name [null] or password is invalid.
所以连接目的地时使用了用户名和密码空值。我认为可能没有正确设置目的地解析器,但我卡在了如何解决这个问题上。有人能帮我解决吗?
我的AppConfig:
@Autowired
private Environment env;
@Autowired
private BeanFactory springContextBeanFactory;
@Bean
public DefaultJmsListenerContainerFactory myListenerContainerFactory() throws NamingException {
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, env.getProperty("java.naming.factory.initial"));
props.setProperty(Context.PROVIDER_URL, env.getProperty("java.naming.provider.url"));
props.setProperty(Context.SECURITY_PRINCIPAL, env.getProperty("java.naming.security.principal"));
props.setProperty(Context.SECURITY_CREDENTIALS, env.getProperty("java.naming.security.credentials"));
Context jndiContext = new InitialContext(props);;
ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("ConnectionFactory");
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setDestinationResolver(new BeanFactoryDestinationResolver(springContextBeanFactory));
factory.setPubSubDomain(true);
factory.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
return factory;
}
听众:
@JmsListener(containerFactory = "myListenerContainerFactory", destination = "topic.FromJndiProperties")
public void receiveMessage(String message) {
try {
System.out.println("Received <" + message + ">");
} catch (Exception e) {
e.printStackTrace();
}
}