I am trying to connect to solace queues and simply read messages from them. However, I am able to read the messages but the messages are not getting removed from the queue.
Below is the code which I tried:
public void clearMessages() throws Exception {
// Programmatically create the connection factory using default settings
// Create connection to the Solace router
SolXAConnectionFactoryImpl connectionFactory = returnConnFactory();
XAConnection connection = connectionFactory.createXAConnection();
XASession session = connection.createXASession();
Queue queue = session.createQueue(QUEUE_NAME);
connection.start();
MessageConsumer messageConsumer = session.createConsumer(queue);
//session.b
messageConsumer.setMessageListener(new MessageListener() {
#Override
public void onMessage(Message message) {
if(message instanceof SolTextMessage) {
SolTextMessage solTextMessage =(SolTextMessage)message;
try {
System.out.println("Message cleared is : "+solTextMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
System.out.println("Message Content: %s"+ SolJmsUtility.dumpMessage(message));
}
latch.countDown();
}
});
latch.await(120,TimeUnit.SECONDS);
connection.stop();
messageConsumer.close();
session.close();
connection.close();
}
Here latch is the object of CountDownLatch which is initialized as:
CountDownLatch latch = new CountDownLatch(2);
You need to commit the XA transaction in order for messages to be consumed.
In JMS, the function call is XAResource.commit(xid, true)
Also, is there a reason to use the CountDownLatch?
If you would like to consume messages synchronously, you can choose not to set a message listener and call MessageConsumer.receive()
Solace does provide a basic sample showing how to make use of XA transactions.
Refer to XATransactions.java in the samples directory of the API.
Note that the sample code is manually managing the XA transaction by calling the relevant XAResource methods such as XAResource.commit().
XA transactions are usually used within Java EE application servers that contain a transaction manager to manage the lifecycle of XA transactions.
Related
We're using hornetQ, and got a separate java application which connects to the queue to read from it.
We're having a Connection to it, creating a JMS session from the connection,
then obtaining a JMS MessageConsumer from the session, then assigning a custom MessageListener to the MessageConsumer.
We call message.acknowledge() for every processed message.
This all works very well, but in the case where we kill our java-application in the middle of processing, before hitting acknowledge(), the message is removed from hornetQ, yet never processed, and lost forever.
It seems that our current java application JMS setup somehow works by removing from the queue, and then rolling back/re-inserting it, if something goes wrong.
But if we kill our application, there won't be any roll-back or re-inserting, as it happens within our client/java-code.
Is there a setting or delivery-mode or something I can configure in the client, that causes the HornetQ to treat every message as "not consumed"(and still in the queue) untill the client sends the ack, so that a rollback arent neccecary?
EDIT: Also tried using transactional mode, calling:
connection.createSession(true, acknowledgementType)
Our MessageListener:
#Override
public void onMessage(Message message) {
LOGGER.debug("Message received. Going to handle message.");
try {
final TextMessage messageReceived = (TextMessage) message;
final String text = messageReceived.getText();
LOGGER.debug(String.format("Message content:\n%s", text));
final boolean isMessageSent = eventDispatcher.handleEvent(text);
if (isMessageSent) {
LOGGER.error("Behold!");
message.acknowledge();
session.commit();
} else {
session.rollback();
}
} catch (JMSException jmse) {
LOGGER.error("Failed to get text from a message from the events queue.", jmse);
} catch (JAXBException jaxbe) {
LOGGER.error("Failed to deserialize contents of the message from the events queue.", jaxbe);
} catch (Exception ex) {
LOGGER.error("An error occurred when receiving an event from the events queue:", ex);
}
I have set up a simple request/reply type scenario (JavaEE 7/Netbeans 7 with Glassfish) with code as follows:
This is a stateless session bean performing the JMS producer role
destination and factory are injected into the EJB.
public void doStuff(int id) {
try {
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
ObjectMessage message = session.createObjectMessage();
MyObject obj = new MyObject(id);
connection.start();
TemporaryQueue replyQueue = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(replyQueue);
message.setJMSReplyTo(replyQueue);
message.setObject(obj);
producer.send(message, DeliveryMode.PERSISTENT, Message.DEFAULT_PRIORITY, 1800000);
Message reply = consumer.receive();
producer.close();
session.close();
connection.close();
} catch (JMSException ex) {
}
}
My problem is that when consumer.receive() is called, execution blocks as expected - however the consumer of this message never runs. The consumer of the original message (it's an MDB) is located in another netbeans project deployed on the same Glassfish server.
I have tried debugging both projects (both the producer and consumer) and it seems like while my producer project is blocking, my consumer project doesn't do anything at all.
If I change consumer.recieve() to something like consumer.receive(20000) then as expected nothing happens for 20 seconds - but as soon as the timeout expires all of a sudden I get a debugger breakpoint hit on my consumer project and it processes as normal. But of course no reply is sent back to my producer project because it's no longer listening!
My other project (the consumer project) looks something like this:
#MessageDriven(activationConfig = {
#ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
#ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "jms/myQueue")
})
---------------------
#Override
public void onMessage(Message message) {
try {
ObjectMessage objMessage = (ObjectMessage) message;
MyObject obj = (MyObject) objMessage.getObject();
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(message.getJMSReplyTo());
connection.start();
try {
myMethod(obj);
} catch (Exception e) {
Message response = session.createTextMessage(e.getMessage());
producer.send(message.getJMSReplyTo(), response);
}
} catch (JMSException ex) {
}
}
How can I get the receive() method to work correctly? I need it to pause for a reply, but while pausing I need my other project to continue working - and I'd assume they're on different threads anyway if they're two separately deployed projects.
I eventually discovered that I was getting this issue because my doStuff() method was running a Container Managed Transaction.
In addition to this, you can only perform one action per transaction (eg: Only one of the request/receive)
So when I was calling producer.send(), the message wasn't actually sent because the transaction had not finished yet (the method was not finished). It then went on to the receive call and blocked there since no message was sent.
I worked around this by making my doStuff() method a Bean Managed Transaction, I injected a UserTransaction object with the #Resource annotation on the bean.
I went to bound the send action by UserTransaction.begin and UserTransaction.commit and I repeated this to bind my second receive action - it worked as expected.
This provided me with a clear explanation on CMTs: http://docs.oracle.com/cd/E19798-01/821-1841/bncij/index.html
i want to do a throttling to a consumer of some queue in the activeMQ, in hornetq (of jboss, this is do it with annotations on the definition of the mdb Consumer). I can't find any similar in the documentation of activemq, the closest that i find was this
consumer.recvDelay 0 ms Pause consumer for recvDelay milliseconds with each message (allows consumer throttling).
from: http://activemq.apache.org/activemq-performance-module-users-manual.html
But there i can't find how i can do it in java.
Thanks in advance,
Regards.
EDIT: Here is the ActiveMQManager code and the consumer code:
public class ActiveMQManager {
private static ActiveMQConnectionFactory CONNECTION_FACTORY;
public static Connection CONNECTION;
public static Session SESSION;
public static Destination TEST_QUEUE;
public static void start() {
try {
CONNECTION_FACTORY = new ActiveMQConnectionFactory("vm://localhost");
CONNECTION = CONNECTION_FACTORY.createConnection();
CONNECTION.start();
SESSION = CONNECTION.createSession(false,
Session.CLIENT_ACKNOWLEDGE);
TestClient testClient = new TestClient();
TEST_QUEUE = SESSION.createQueue("TEST.QUEUE");
MessageConsumer testConsumer = SESSION.createConsumer(TEST_QUEUE);
test.setMessageListener(testClient);
} catch (Exception e) {
}
}
public static void stop() {
try {
// Clean up
SESSION.close();
CONNECTION.close();
} catch (JMSException e) {
log.error(e);
}
}
}
The consumer code is very simple (for this example):
public class TestConsumer implements MessageListener {
#Override
public void onMessage(Message message) {
//Do something with the message
}
}
this depends on the consumer technology being used...but here are a few options
you can manually introduce a delay in your consumer code (not an exact science but this will limit the throughput)
you can also control the number of threads that your consumer uses by setting maxConcurrentConsumers property of you JMS connection...that said, this won't throttle message throughput, just limit the level of concurrency being used by your consumer
better yet, you can set the exact number of messages to consume per time period using a throttler EIP implementation
for example, this is trivial using the Camel Throttler
from("activemq:queueA").throttle(10).to("activemq:queueB")
With ActiveMQ you can set the consumer prefetch limit: http://activemq.apache.org/what-is-the-prefetch-limit-for.html
To configure it, you can use the connection URL (most of the configurations can be done using the URL) or the Java API.
For more interesting parameters: http://activemq.apache.org/connection-configuration-uri.html
Take into account that Camel Throttler keeps the exchanges in-memory while they are blocked by the Throttler. So, if you have 100 queue consumers and the Server (exposing the SOAP service) is slow, you may have in-memory up to 100 exchanges!
Posted this question for this: Throttle consumption rate of all JMS consumers listening on an ActiveMQ queue
Currently there is a embedded broker running in my application, and I want to consume the queue within the same application in different thread. It works when I use TCP Transport, but I found I can not use VM Transport when the broker and the consumer in the same application. (It works if I create another process for consumer) Is there a better way to do in my situation?
Broker config with Spring
<amq:broker id="myBroker" brokerName="myBroker">
<amq:transportConnectors>
<amq:transportConnector uri="tcp://localhost:7777" />
<amq:transportConnector uri="vm://myBroker" />
</amq:transportConnectors>
</amq:broker>
Consumer
public class TestConsumer {
private static String brokerURL = "tcp://localhost:7777";
private static transient ConnectionFactory factory;
private transient Connection connection;
private transient Session session;
public TestConsumer() throws JMSException {
factory = new ActiveMQConnectionFactory(brokerURL);
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}
public void close() throws JMSException {
if (connection != null) {
connection.close();
}
}
public Session getSession() {
return session;
}
}
Listener
public class Listener implements MessageListener {
public void onMessage(Message message) {
try {
//do something here
System.out.println(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
In Main
TestConsumer consumer = new TestConsumer();
Destination destination = consumer.getSession().createQueue("TESTQUEUE");
MessageConsumer messageConsumer = consumer.getSession().createConsumer(destination);
messageConsumer.setMessageListener(new Listener());
It works when brokerURL is "tcp:localhost:7777" or is "vm://myBroker" but Broker and Consumer are in different processes. I just can not use VM Transport when the two are in the same application.
Try to use VM trnasport. If I understand you correctly your application sends and receives messages itself. In this case VM transport is the best choice: it even does not exit your JVM and transfers messages using direct API calls.
See this article for details.
http://activemq.apache.org/configuring-transports.html
I'm testing open MQ for send and receive messages in my project. I have no problem to configure it to send a synchronous message, but i can't find any way in the official documentation to configure the message to be consumed 15 minutes after the producer send a message, and continue call the consumer if an error appears.
offical documentation: http://dlc.sun.com/pdf/819-7757/819-7757.pdf
my method whom send a message
public void sendMessage(EntradaPrecomven entrada){
try{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL, "file:///C:/mqteste");
// Create the initial context.
Context ctx = new InitialContext(env);
// Look up the connection factory object in the JNDI object store.
autenticisFactory = (ConnectionFactory) ctx.lookup(CF_LOOKUP_NAME);
mdbConn = autenticisFactory.createConnection();
mdbSession = mdbConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = (Destination) ctx.lookup(DEST_LOOKUP_NAME);
MessageProducer myProducer = mdbSession.createProducer(destination);
ObjectMessage outMsg = mdbSession.createObjectMessage(entrada);
outMsg.setJMSRedelivered(Boolean.TRUE);
myProducer.send(outMsg);
consumidor = mdbSession.createConsumer(destination);
MessageMDB myListener = new MessageMDB();
consumidor.setMessageListener(myListener);
mdbConn.start();
mdbConn.close();
}catch(Exception e){
try {
mdbSession.rollback();
} catch (JMSException e1) {}
}
}
My listener:
#Override
public void onMessage(Message msg) {
ObjectMessage objMessage = (ObjectMessage) msg;
try {
System.out.println("Received Phone Call:" + objMessage.getJMSRedelivered());
throw new JMSException("TESTE");
} catch (JMSException e) {
e.printStackTrace();
}
}
So, when i call mdbConn.start() the sendMessage() is called, but i want to call 15 minutes after the call. And whatever it sendMessage() does, the message is always removed from the queue. How can i keep the messagen in queue to be called later ?
Thanks!
The message is removed from the broker queue due to the fact that the session you are using is set to auto acknowledge.
mdbSession = mdbConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
This will automatically send an acknowledgement to the broker that the listener has received a message for the consumer with which it is associated once the onMessage() method has executed to completion. This then results in the message being removed from the queue.
If you manually take over the acknowledgement process you can choose to only acknowledge the receipt of the message at a time of your choosing (be that 15 minutes later or whatever criteria you have for the consuming client).
Setting the Session Session.CLIENT_ACKNOWLEDGE will allow you to do this but then you will have to manually send an acknowledge in your consumer code. By calling acknowledge on the message msg.acknowledge() inside your onMessage() method within your listener.
This will then acknowledge the receipt of messages consumed within that session and remove them from the queue.
Pages 46 and 65 in the pdf you quoted are useful for more information on this as is the api