使用Spring 4.2注释和ActiveMQ对@JmsListener进行身份验证

时间:2016-02-25 15:23:25

标签: java spring jms activemq

尝试使用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.

因此,使用用户名和密码null建立与目标的连接。我想我可能没有正确设置destinationResolver但我不知道如何解决这个问题。任何人都可以帮我解决这个问题吗?

我的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();
    }
}

2 个答案:

答案 0 :(得分:1)

如果您尚未更改代理端的任何功能,请尝试以下

  >>python foo.py

也可以在你的jndi.properties中添加属性userName = admin和password = admin

答案 1 :(得分:0)

ConnectionFactoryTopicConnectionFactoryQueueConnectionFactory都有两种创建连接的方式(例如ConnectionFactory.createConnection()ConnectionFactory.createConnection(username, password)

似乎@JmsListener批注始终会创建一个连接,而不通过零args方法传递凭据。

我使用spring的UserCredentialsConnectionFactoryAdapter来包装ConnectionFactory以供@JmsListener使用,以便它在创建连接时传递凭据