使用用户名/密码验证与 activeMQ 的连接

发布于 2024-11-29 00:49:58 字数 1514 浏览 2 评论 0原文

我有一个应用程序运行正常,可以向 activemq 发送消息。我正在使用 spring.net 和 Nmstemplate 连接到代理。 xml 配置文件一般是:

<object id="ActiveMqConnectionFactory" 
        type="Apache.NMS.ActiveMQ.ConnectionFactory, Apache.NMS.ActiveMQ">
</object>

<object id="ConnectionFactory"
        type="Spring.Messaging.Nms.Connections.CachingConnectionFactory, Spring.Messaging.Nms">
  <constructor-arg index="0" ref="ActiveMqConnectionFactory"/>
  <property name="SessionCacheSize" value="10"/>
</object>

<object id="NmsTemplate" 
        type="Spring.Messaging.Nms.Core.NmsTemplate, Spring.Messaging.Nms">
  <constructor-arg index="0" ref="ConnectionFactory"/>
  <property name="MessageConverter" ref="SimpleMessageConverter"/>
</object>

<object id="SimpleMessageConverter" 
        type="Spring.Messaging.Nms.Support.Converter.SimpleMessageConverter, Spring.Messaging.Nms">
</object>

直到一切正常,找到使用 NmsTemplate.ConvertAndSend() 发送消息; 问题是我想使用用户名/密码保护连接。 我在 activemq 配置文件中设置了凭据,现在我需要在代码中提供此凭据,但我找不到在哪里! 我尝试过:

<object id="ActiveMqConnectionFactory" type="Apache.NMS.ActiveMQ.ConnectionFactory, Apache.NMS.ActiveMQ">
  <property name="UserName" value="usertest"/>
  <property name="Password" value="passwordtest"/>
</object>

但是在发送时我收到“连接已关闭”异常,并且代码中设置了相同的凭据。

那么,有人有关于如何设置用户名/密码以将消息发送到安全的 activemq 代理的很好的示例或提示吗?

I have an application running ok sending messages to activemq. I'm using spring.net and Nmstemplate to connect to broker.
xml configuration file in general is:

<object id="ActiveMqConnectionFactory" 
        type="Apache.NMS.ActiveMQ.ConnectionFactory, Apache.NMS.ActiveMQ">
</object>

<object id="ConnectionFactory"
        type="Spring.Messaging.Nms.Connections.CachingConnectionFactory, Spring.Messaging.Nms">
  <constructor-arg index="0" ref="ActiveMqConnectionFactory"/>
  <property name="SessionCacheSize" value="10"/>
</object>

<object id="NmsTemplate" 
        type="Spring.Messaging.Nms.Core.NmsTemplate, Spring.Messaging.Nms">
  <constructor-arg index="0" ref="ConnectionFactory"/>
  <property name="MessageConverter" ref="SimpleMessageConverter"/>
</object>

<object id="SimpleMessageConverter" 
        type="Spring.Messaging.Nms.Support.Converter.SimpleMessageConverter, Spring.Messaging.Nms">
</object>

Until everything is working find sending message with NmsTemplate.ConvertAndSend();
The problem is that I want to secure connection using username/password.
I setup credentials in activemq configuration file and now I need to supply this credentials in code but I dont find where !!
I tried with:

<object id="ActiveMqConnectionFactory" type="Apache.NMS.ActiveMQ.ConnectionFactory, Apache.NMS.ActiveMQ">
  <property name="UserName" value="usertest"/>
  <property name="Password" value="passwordtest"/>
</object>

But when sending I get "Connection already closed" exception, and the same setting credentials in code.

So, anyone have a good example or hint in how to setup username/password to send message to secured activemq broker?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酸甜透明夹心 2024-12-06 00:49:59

由于没有人回答这个问题,我在 http://forum.springframework.net/showthread.php?9184-authenticate-to-activeMQ-using-Nmstemplate-in-net

this.ConnectionFactory.UserName = this.Username;
this.ConnectionFactory.Password = this.Password;

this.ConnectionFactory.BrokerUri = new System.Uri(this.Uri);

using (IConnection conn = this.ConnectionFactory.CreateConnection())
{
using (ISession session = conn.CreateSession())
{
    IObjectMessage objMessage = session.CreateObjectMessage(message);
    using (IMessageProducer producer = session.CreateProducer())
            {
                    NmsDestinationAccessor destinationResolver = new NmsDestinationAccessor();
                    IDestination destination = destinationResolver.ResolveDestinationName(session, this.Queue);
                    producer.Send(destination, objMessage);
            }
    }
}

Since no-one has answered this, I found this answer on http://forum.springframework.net/showthread.php?9184-authenticate-to-activeMQ-using-Nmstemplate-in-net

this.ConnectionFactory.UserName = this.Username;
this.ConnectionFactory.Password = this.Password;

this.ConnectionFactory.BrokerUri = new System.Uri(this.Uri);

using (IConnection conn = this.ConnectionFactory.CreateConnection())
{
using (ISession session = conn.CreateSession())
{
    IObjectMessage objMessage = session.CreateObjectMessage(message);
    using (IMessageProducer producer = session.CreateProducer())
            {
                    NmsDestinationAccessor destinationResolver = new NmsDestinationAccessor();
                    IDestination destination = destinationResolver.ResolveDestinationName(session, this.Queue);
                    producer.Send(destination, objMessage);
            }
    }
}
冷了相思 2024-12-06 00:49:59

尝试以下操作,它会使用 userName & 创建一个连接。 password 然后启动它创建 session,然后用于创建一个使用 consumer.Receive()consumer从队列中读取单个消息。


string brokerUri = "broker uri here";
string userName = "someone";
string password = "**********";
string QueueName = "somequeue";

IConnectionFactory connectionFactory = new ConnectionFactory(new Uri(brokerUri));

//Create Connection
using (var connection =  connectionFactory.CreateConnection(userName,password)
{
    connection.Start(); //Start Connection

    //Creating new session
    using (var session = connection.CreateSession()){
        //Getting the Queue
        var queue = session.GetQueue(config.QueueName);
        
        //Using destination: 
        IDestination destination = Apache.NMS.Util.SessionUtil.GetDestination(session, $"queue://{queue.QueueName}");

        //Create a new consumer
        using (IMessageConsumer consumer = session.CreateConsumer(destination)){
            try{
                // Consume a message
                ITextMessage message = consumer.Receive(timeSpan) as ITextMessage;
                /*Do something here with message*/              
            }
            catch (NMSConnectionException nmex){
                //LogError(nmex.Message, nmex);
            }
            catch (Exception ex){
                log.LogError(ex.Message, ex);
            }
        }
    }
}

Try the following, it creates a Connection with userName & password then starts it to create as session which then used to create a consumer which uses consumer.Receive() to read a single message form the queue.


string brokerUri = "broker uri here";
string userName = "someone";
string password = "**********";
string QueueName = "somequeue";

IConnectionFactory connectionFactory = new ConnectionFactory(new Uri(brokerUri));

//Create Connection
using (var connection =  connectionFactory.CreateConnection(userName,password)
{
    connection.Start(); //Start Connection

    //Creating new session
    using (var session = connection.CreateSession()){
        //Getting the Queue
        var queue = session.GetQueue(config.QueueName);
        
        //Using destination: 
        IDestination destination = Apache.NMS.Util.SessionUtil.GetDestination(session, 
quot;queue://{queue.QueueName}");

        //Create a new consumer
        using (IMessageConsumer consumer = session.CreateConsumer(destination)){
            try{
                // Consume a message
                ITextMessage message = consumer.Receive(timeSpan) as ITextMessage;
                /*Do something here with message*/              
            }
            catch (NMSConnectionException nmex){
                //LogError(nmex.Message, nmex);
            }
            catch (Exception ex){
                log.LogError(ex.Message, ex);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文