authenticate connection to activeMQ with username/password
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="MessageConve开发者_开发技巧rter" 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?
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);
}
}
}
Try the following, it creates a Connection with userNam
e & 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, $"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);
}
}
}
}
精彩评论