How to set Async send with JmsPooledConnectionFactory in ActiveMQ - activemq

In ActiveMQ in Action the author states as one of the performance tuning possibilities for ActiveMQ is to set the AsyncSend flag for ActiveMQConnectionFactory. In my case I want to use the JmsPoolConnectionFactory to create a pool of connection and in the same time set the AsyncSend flag. However, I cannot seem to find a way to to that.

You can set jms.useAsyncSend=true on the connection's URL as described in the documentation, e.g.:
tcp://locahost:61616?jms.useAsyncSend=true

Depending on which pooling implementation you are using the way it is created might differ a bit but in general they all just accept an external JMS ConnectionFactory instance and then create JMS resources from there. So this implies that if you want to set configuration that is specific to your own JMS provider then you should be doing that through the ConnectionFactory that you pass to the pool's ConnectionFactory proxy.

Related

How to configure XA transaction timeout with Apache Active (Amazon) MQ?

I'm trying to set up JMS client to use XA transactions with variable timeouts, talking to Active MQ classic (Amazon MQ with ActiveMQ broker 5.15.4)
Calling jmsSession.getXAResource().setTransactionTimeout(timeOut) returns false, indicating that that didn't succeed. This means that sending SESS_XA_SET_TIMEOUT_RESP to the broker resulted in !OK, but I don't see any logs that would explain why (for that matter, so far I can't see any logs from the broker). The same XA logic code works just fine with Artemis.
I found Resource Manager Configuration that says that [default] transaction timeout can be set in the broker configuration file, however it's unclear how exactly this is to be specified, the configuration file schema has no mention of transaction-timeout property, nor can I see any "generic" property list where can it be specified.
So two-part question, really:
How do I configure the "default" transaction timeout mentioned in Resource Manager Configuration
Is there a particular reason I can't change the transaction timeout programmatically.
OK, the short answer - ActiveMQ Classic (5.x) doesn't support distributed transaction timeouts.
I apparently was looking at Artemis's client code, instead of the Classic's. In the Classic client, TransactionContext.setTransactionTimeout() simply returns false:
#Override
public boolean setTransactionTimeout(int seconds) throws XAException {
return false;
}
As far as I can understand, this means that XA transactions never timeout, but I can't find any documentation that talks about any of it.

How to retrieve JMS Administered Objects from (GlassFish) server

My Glassfish server is up and running and I can run simple JMS client programs that send and retrieve messages to queues and topics that are configured in the Glassfish console manually.
I'm busy writing a simple JMSBrowser to study JMS and would like to find out how I can retrieve from a java client the names of the administered objects Server objects (ConnectionFactory, Queues & Topics).
For example I have ConnectionFactory with JNDI name jms/__defaultConnectionFactory, a Queue jms/GlassFishBookQueue and a topic jms/GlassFishBookTopic.
How can I retrieve these names, when I only know there resource types (javax.jms.ConnectionFactory, javax.jms.Queue and javax.jms.Topic)
In this example I have one of every kind, but each could of course be a list.
Spent a lot of time trying to figure it out, to no avail.
It should be possible as any JMSBrowser present this information, so for instance this screen shot:
https://sourceforge.net/projects/jmstoolbox/
Any hint would be appreciated.
I'm the author of JMSToolBox
The JMS specs does not define a way to play with JMS artefacts defined in a server, ie create/delete/list..Queues/Topics/factories etc
Each Queue Manager has it's own proprietary way to expose those features. In JMSToolBox, I usually use JMX for that (with the help of proprietary MBeans..), but sometimes it is proprietary code to connect and list those objects.
If you connect to the Queue manager server via JNDI, there is probably a way to list all the JMS artefacts from the JNDI tree, and based on some proprietary pattern, determine what "kind" of object they represent(Queue, ConnectionFactory etc..)
BTW, GlassFish embeds OpenMQ. Here is the way it is done in JMSToolBox
I hope this helps

Queue declaration on all nodes in RabbitMQ

I have a Rabbitmq cluster setup (without HA). My consumers are spring applications and it provides a failover mechanism out of the box where it connects to the next available node.
Since the queues are not mirrored, is it okay if I declare the queues up front and when the first node goes down, the connection will be established to the second node. Does this make sense?
Another question, lets say I have a load balance on top of Rabbitmq cluster. My applications connect using the load balance. Will the queues be declared on all nodes or will it be declared on the node based on the routing strategy by LB.
For the first scenario, yes, the queues will be declared on the failover broker instance when the connection is established.
If you want to pre-declare on all nodes you will need a connection factory for each node, and a RabbitAdmin for each connection factory.
You will also need something to cause a connection to be opened on each (the RabbitAdmins register themselves as connection listeners).
You can do that by adding a bean the implements SmartLifecycle and call createConnection() on each connection factory.
You can also selectively declare elements. See Conditional Declaration.
By default, all queues, exchanges, and bindings are declared by all RabbitAdmin instances (that have auto-startup="true") in the application context.
Starting with the 1.2 release, it is possible to conditionally declare these elements. This is particularly useful when an application connects to multiple brokers and needs to specify with which broker(s) a particular element should be declared.

create list of activemq queue

my existing code which uses BlockingQueue creates a list of BlockingQueue (like private List> queues;) in which I can put messages to process.
However due to persistence issue we plan to shift to activemq.
Can anyone please help me if we can get a list of activemq queue (in java program not from configuration file). I know that I can use createQueue on session to create a single instance of the queue but I want list of queue like done for BlockingQueue.
Any help would be much appreciated.
You can get a list of the available queues using DestinationSource from your connection.
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
ActiveMQConnection connection = (ActiveMQConnection)connectionFactory.createConnection();
DestinationSource ds = connection.getDestinationSource();
Set<ActiveMQQueue> queues = ds.getQueues();
edit:
to create a queue take a look at ActiveMQ Hello world sample link What the code does there is creating a connection to an activeMQ-broker embedded in the jvm
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("TEST.FOO");
The thing that might not be obvious with above code is that the line:
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
will not only setup a connection to a broker, but embedded a broker inside the connection if there isn't one already. Explained at the bottom of this page
That feature can be turned of using (you need a broker, but if you want to set it up in other way):
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?create=false");
I really like ActiveMQ, but it offers a lot more than persistence, so things might seem a little overly complex when doing the simple things. But hope that will not scare you.
To create a list of queues, you have to create that list, then create each queue individually from the session object.
Queue q = session.createQueue("someQueueName")
This, however, does not really "create" a queue in that sense, since a queue is a persistent "thing" in the ActiveMQ process/server. This will only create a reference to an ActiveMQ queue given an identifier/name.
I'm not sure why you need ten queues right up. Typically, you have one queue per event type or use case (or similar), then use concurrent consumers to process in parallel.
But of course, you can always do somethings similar by a simple for loop, creating one queue at a time and attaching them to an arraylist. Note that you cannot get type safe queues with only Event objects in them.
You can send ObjectMessages with events though. Just create one:
Event event = createEvent(..); // Given "Event" is serializable (need to be able to persist it).
Message m = session.createObjectMessage(event);
// Send message as usual in ActiveMQ.
It might be that you need to rethink one or a few things in your code when converting from BlockingQueues to persistent ActiveMQ queues.

ActiveMQ producer creation and inactivity periods

I have a question relating ActiveMQ and producers.
Should I create a producer for every sending of a message? Or use the same one all the time? Is there a performance impact by creating a producer for every sending?
Also the connection gets down after some period of inactivity, but I don't know if it's related to this, any advices?
Yes, there is a small performance impact in creating a producer, especially if the broker is located on another machine (the clients needs to talk to broker to create a producer).
In the rest of this answer I assume you use Java/JMS to talk with AMQ.
If you have a very trivial program, you could of course "re use" your producers, create them with a "NULL" destination, and set the destination when sending.
What you could do to make it easy is to use the PooledConnectionFactory which pools connections, sessions and producers. I think that wrapper class will help you.
Actually, you could use the PooledConnectionFactory like this (psuedocode):
cf = new PooledConnectionFactory(myOriginalConnectionFactory)
sendMessage(cf)
sendMessage(cf)
sendMessage(cf)
SendMessage(connectionFactory)
conn = connectionFactory.CreateConnection
sess = conn.CreateSession
prod = sess.createProducer
msg = sess.createMessage
prod.send(msg)
prod.close
sess.close
conn.close
This means you don't have to worry about closed/open sessions, connections etc. This is the way the widely used JmsTemplate from Spring Framework works (and of course works a lot better with pooled/cached resources).
Also look at this page for performance tips and tricks.