I already posted a question on this topic some two days earlier, but it was much more complicated question. Now, to put it more simply:
Is there a way to obtain username, used to connect the broker, in spring JMS listener? I have found only MessageListener and SessionAwareMessageListener, but I'm not able to obtain this information (or Connection instnace) from neither Session, nor Message instance.
Am I missing something obvious?
I'm using ActiveMQ embedded broker.
OK, the obvious answer was, that the connection listener is on is not the same connection as the client is on... Therefore, there is NO POINT of obtaining the connection handle...
Stupid me...
PS: I'm leaving it here to warn the future generations.
How about?
#JmsListener(destination = "mytopic")
public void processMessage(BytesMessage message) {
ActiveMQBytesMessage msg = (ActiveMQBytesMessage) message;
ActiveMQConnection conn = msg.getConnection();
ConnectionInfo info = conn.getConnectionInfo();
String username = info.getUserName();
}
Related
I want to know the number of queues on my ActiveMQ and I have this Code, which works with my local installation of ActiveMQ:
ActiveMQSslConnectionFactory connectionFactory = new ActiveMQSslConnectionFactory(url);
//connectionFactory.setTrustStore("truststore.ts");
//connectionFactory.setTrustStorePassword("password");
ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection();
connection.start();
DestinationSource destinationQueues = connection.getDestinationSource();
Set<ActiveMQQueue> queues = destinationQueues.getQueues();
System.out.println(queues.size());
But when I'm using it on our ActiveMQ on the server (with the truststore) it will return zero queues. I know that the connection works because I can consume messages from one exact queue. I already tried tips from other threads like sleep but it still won't work.
Is it maybe because of the SSL Connection, do I have to change something in this case? Thank you in advance.
Found the problem. It's a setting of the ActiveMQ which is found in the activemq.xml. The part advisorySupport="false" caused the problem. After commenting this setting out, the query returns the true amount of queues.
The Destination Source bits make no guarantee of timely return of the full set of Queues etc. It could be you have configured the SSL connector on the broker differently or it could just be that due to the much slower throughput of SSL on JDK implementations the data hasn't arrived.
I am having a problem about JMS. The problem is, I have an application and it is trying to send a message through JMS , but after JMS server restart, it throws exception as when the server was down time. It is not reconnecting.
It is completely fine without a restart of JMS server and I am using weblogic 10.x.
Is it a problem about JMS configuration?
Thanks
Can you post your code how you are sending messages?
If you attempt to send a message when the JMS server is down, you will probably have an exception, and have to deal with that. When you attempt to send a message the next time, when the JMS server is restarted and running, you probably create a new connection from your connection factory. Reconnection will happend then:
// Let's say, you inject CF and Dest.
#Resource(lookup = "jms/ConnectionFactory")
private static ConnectionFactory cf;
#Resource(lookup = "jms/MyQueue")
private static Destination dest;
public void sendMessage(){ // called every time you need to send a message.
try{
Connection con = cf.createConnection(); // will reconnect, otherwise pooled.
Session sess = con.createSession(false,Session.AUTO_ACKNOWLEDGE);
MessageProducer prod = sess.createProducer(dest);
Message msg = sess.createTextMessage("Hello, World");
prod.send(msg);
sess.close();
con.close();
}catch(Exception e){
// handle errors
}
}
However, there are some built in failover/reconnecting features in Weblogic JMS, take a look at this page:
Oracle code listing and documentation about reconnecting JMS producers
I need to send JMS messages to the following provider location:
failover:(tcp://amq.vip.ebay.com:61616,tcp://amqstby.vip.ebay.com:61616)?initialReconnectDelay=100&randomize=false&wireFormat.maxInactivityDuration=0
How to correctly initialize ConnectorFactory for it? Should I just do the follwing?
String url = "failover:(tcp://amq.vip.ebay.com:61616,tcp://amqstby.vip.ebay.com:61616)?initialReconnectDelay=100&randomize=false&wireFormat.maxInactivityDuration=0";
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Or the things are more tricky with this kind of provider urls?
That is the correct syntax. Be careful when turning off the inactivity monitor.
My question is pretty simple, but I am having a bad time finding a solution.
I want to be able to get all the queues in the server (or session, that would also be ok). Is that possible?
My situation is the following:
I am new to openMQ, Glassfish, activeMQ and so on. I developed a monitoring system for activeMQ in which I get all the existing queues and show to the user, so it can get information about number of messages and so on.
To do that, I have this code:
ActiveMQConnection.makeConnection("tcp://localhost:61616");
activeMQConnection.start();
//Get queues
DestinationSource destinationSource = activeMQConnection.getDestinationSource();
Set<ActiveMQQueue> queues = destinationSource.getQueues();
this last line gets all the queues for the connection, and this is exactly what I need. But this was my code for ActiveMQ.
Now the team decided to change to openMQ, and I have to adapt my monitoring system to be able to handle that. I would like to use LDAP so I can do it technology-independent. After a lot of research I came to this code:
ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("cn=QueueConnectionFactory");
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue myQueue = session.createQueue("myQueue");
This is good, because it would be completely independente (I think, have to test it). But the problem is that I have to register the queue, something I would not like to do. I would like to get all the existing queues in the server without any need for registering, but I can't find any GetQueues() method or any way to mimic its behavior.
I also found out the the DestinationSource class in activeMQ inherits from MessageListener, but this class doesn't provides any similar method :(
Could you please help me?
thank you,
Oscar
I was able to do that using JMX, here is the code:
HashMap environment = new HashMap();
String[] credentials = new String[] { "user", "pass" };
environment.put(JMXConnector.CREDENTIALS, credentials);
JMXServiceURL url = new JMXServiceURL("URL");
// Get JMX connector, supplying user name and password
JMXConnector jmxc1 = JMXConnectorFactory.connect(url, environment);
// Get MBean server connection
MBeanServerConnection mbsc = jmxc1.getMBeanServerConnection();
ObjectName destMgrConfigName = new ObjectName(MQObjectName.DESTINATION_MANAGER_MONITOR_MBEAN_NAME);
// Create operation's parameter and signature arrays
Object opParams[] = {};
String opSig[] = {};
// Invoke operation
ObjectName[] objectNames = (ObjectName[]) mbsc.invoke(destMgrConfigName, DestinationOperations.GET_DESTINATIONS, opParams, opSig);
for (ObjectName objectName : objectNames) {
System.out.println(objectName.getCanonicalName());
System.out.println(objectName.getKeyProperty("name"));
}
more references here: http://forums.oracle.com/forums/thread.jspa?threadID=2129291&tstart=0
I haven't touched any J2EE stuff in years and I need to whip up a quick JMS client for a demo.
I'm using Eclipse, on OS X and I can't even get started because I can't seem to figure out how to get the required libraries.
This is supposed to be a simple stand alone application (not running in a container) that pulls messages from a topic.
Every JMS implementation has its own set of libraries that specify how you get the initial connection factory. If you have an existing server from which to pull messages, you need to examine the documentation of that server to determine where to find the libraries to put in your classpath and how to create your initial connection factory. If you want to create a server for the purposes of the demonstration, I recommend using an embedded Active MQ broker.
Once you have your connection factory, polling for messages from a topic is pretty straightforward. Here is some example code which can be called to drain a topic of its current messages.
// Implementation specific code
public void drainTopic(TopicConnectionFactory factory, String topicName, String subscriberId)
// set factory properties like the server ID
Connection conn = factory.createConnection();
conn.start();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(topicName);
MessageConsumer consumer = session.createDurableSubscriber(topic, subscriberId);
Message message;
while (null != (message = consumer.receive(1000))) {
// do work on the message
}
conn.close();
}
Note the use of a durable subscriber. This means that I don't have to try to maintain a single connection all the time and handle the errors if it times out somehow. But because the subscription is durable, the server knows to retain any messages the topic receives while I'm not connected and provide them on my next connection. This code would be the same regardless of the host OS. The only tricky part is the creation of the provider specific connection factory.