Hi guys,
I have a persistent JMS Q running on WLS 6.1. I have a MessageDrivenBean with an onMessage method that reads the message off the q when it arrives. I am then writing the message to file.
Myquestions.
If I am running under a container managed transaction (which i set in the creation of the Qsession, and in the ejb-jar.xml) am I correct in thinking the container automatically commits the transaction if the message is written to file with no exceptions?
More importantly, if the message is not able to be written to file I want to rollback so the transaction can be started again and the message will be redelivered and therefore not lost.
I call setRollbackOnly on the messageDrivenContext, set up when the mdb is created by the continer. But then my server/jmsQ goes into an infinte loop trying to redeliver my message to the MDB . How can I break out of this loop?
I need to reconsume this message but only after the problem - why it cannot be written to file - has been fixed. Any ideas on how to do that?
I've tried testing to check if the messageDrivenContext has rollbackonly set, by calling getrollback only, but this fails to work!
Any help will be appreciated, my code is below:
*************************************************************
public class JMSConsumer implements MessageListener, MessageDrivenBean {
private static MessageDrivenContext mDC;
public JMSConsumer() {
// JMS Q SETUP HERE, WORKS FINE
}
public void setMessageDrivenContext(MessageDrivenContext mDC) {
this.mDC = mDC;
System.out.println("Setting the MessageDrivenContext of the JMSReceiver");
}
public void ejbCreate() {
System.out.println("In the ejbCreate of the JMSReceiver\n");
}
public void onMessage(Message message) {
String messageID = null;
System.out.println("In the onMessage() of the JMSReceiver");
// Receive all text messages from queue
if (message != null) {
if (message instanceof TextMessage) {
System.out.println("message == instanceof textmessage");
//System.out.println(((TextMessage)message).getText());
if (!mDC.getRollbackOnly()) {
System.out.println("rollback not set on this trans");
boolean result = logTheMessage(message);
if (result) {
System.out.println("result true");
}
else {
System.out.println("result false");
mDC.setRollbackOnly();
ejbRemove();
}
}//
else {
System.out.println("rollback set on this trans");
}
}
else {
System.out.println("message not txt");
}
}
else {
System.out.println("null message: ");
}
}
public void ejbRemove() {
System.out.println("In the ejbRemove of the JMSReceiver");
}
private boolean logTheMessage(Message message) {
boolean result = true;
FileWriter out = null;
File outputFile = new File("c:\\Logger.txt");
try {
out = new FileWriter(outputFile);
}
catch (IOException io) { System.out.println(io.toString());}
try {
String stringMessage = message.toString();
out.write(stringMessage);
}
catch (IOException io){
result = false;
}
finally {
try {
out.close();
} catch (IOException io) {System.out.println(io.toString());}
}
return false;
}
}