User: pkendall
Date: 01/08/15 21:04:40
Modified: src/main/org/jboss/mq SpySession.java
SpyMessageConsumer.java SpyConnectionConsumer.java
Log:
Several subtle bug fixes.
Transacted sessions were not rolled back when closed.
Have to save subscriptions that have unacked messages.
Revision Changes Path
1.2 +16 -12 jbossmq/src/main/org/jboss/mq/SpySession.java
Index: SpySession.java
===================================================================
RCS file: /cvsroot/jboss/jbossmq/src/main/org/jboss/mq/SpySession.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SpySession.java 2001/08/11 20:59:11 1.1
+++ SpySession.java 2001/08/16 04:04:40 1.2
@@ -34,7 +34,7 @@
* @author Norbert Lataille ([EMAIL PROTECTED])
* @author Hiram Chirino ([EMAIL PROTECTED])
*
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
abstract public class SpySession
implements Session, XASession
@@ -153,38 +153,42 @@
sessionConsumer.setMessageListener(listener);
}
- // Optional Connection consumer methods
- java.util.LinkedList messages = new java.util.LinkedList();
+ // Optional Connection consumer methods
+ java.util.LinkedList messages = new java.util.LinkedList();
- void addMessage(SpyMessage message){
+ void addMessage(SpyMessage message){
synchronized(messages){
- messages.addLast(message);
+ messages.addLast(message);
}
- }
+ }
// Delivers messages queued by ConnectionConsumer to the message listener
- public void run(){
+ public void run(){
synchronized(messages){
- while(messages.size() > 0){
+ while(messages.size() > 0){
SpyMessage message = (SpyMessage)messages.removeFirst();
try{
- if(sessionConsumer == null){
+ if(sessionConsumer == null){
cat.warn("Session has no message listener set, cannot process
message.");
//Nack message
connection.send(message.getAcknowledgementRequest(false));
- }else
+ }else
sessionConsumer.addMessage(message);
}catch(JMSException ignore){
+ }
}
- }
}
- }
+ }
public synchronized void close() throws JMSException
{
// allow other threads to process before closing this session
// Patch submitted by John Ellis (10/29/00)
Thread.yield();
+
+ //deal with any unacked messages
+ if(!closed && transacted && spyXAResource == null)
+ rollback();
synchronized (runLock) {
if (closed) return;
1.2 +97 -97 jbossmq/src/main/org/jboss/mq/SpyMessageConsumer.java
Index: SpyMessageConsumer.java
===================================================================
RCS file: /cvsroot/jboss/jbossmq/src/main/org/jboss/mq/SpyMessageConsumer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SpyMessageConsumer.java 2001/08/11 20:59:11 1.1
+++ SpyMessageConsumer.java 2001/08/16 04:04:40 1.2
@@ -29,7 +29,7 @@
* @author Hiram Chirino ([EMAIL PROTECTED])
* @author David Maplesden ([EMAIL PROTECTED])
*
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public class SpyMessageConsumer implements MessageConsumer, SpyConsumer, Runnable {
@@ -42,11 +42,11 @@
// The subscription structure should be fill out by the decendent
public Subscription subscription = new Subscription();
- protected Object stateLock = new Object();
- protected boolean receiving = false;
- protected boolean waitingForMessage = false;
- protected boolean listening = false;
- protected Thread listenerThread = null;
+ protected Object stateLock = new Object();
+ protected boolean receiving = false;
+ protected boolean waitingForMessage = false;
+ protected boolean listening = false;
+ protected Thread listenerThread = null;
//List of Pending messages (not yet delivered)
LinkedList messages;
@@ -76,20 +76,20 @@
throw new IllegalStateException("The MessageConsumer is
closed");
synchronized(stateLock){
- if(receiving)
+ if(receiving)
throw new JMSException("Another thread is already in receive.");
- boolean oldListening = listening;
- listening = (listener != null);
- messageListener = listener;
+ boolean oldListening = listening;
+ listening = (listener != null);
+ messageListener = listener;
- if(!sessionConsumer && listening && !oldListening){
+ if(!sessionConsumer && listening && !oldListening){
//Start listener thread (if one is not already running)
if(listenerThread == null){
- listenerThread = new Thread(this,"MessageListenerThread -
"+subscription.destination.getName());
- listenerThread.start();
+ listenerThread = new Thread(this,"MessageListenerThread -
"+subscription.destination.getName());
+ listenerThread.start();
}
- }
+ }
}
}
@@ -99,22 +99,22 @@
throw new IllegalStateException("The MessageConsumer is
closed");
synchronized(stateLock){
- if(receiving)
+ if(receiving)
throw new JMSException("Another thread is already in receive.");
- if (listening)
- throw new JMSException("A message listener is already
registered");
- receiving = true;
+ if (listening)
+ throw new JMSException("A message listener is already
registered");
+ receiving = true;
}
SpyMessage msg = session.connection.receive(subscription, 0);
if( msg != null ) {
- Message mes = preProcessMessage( msg );
- if( mes != null ){
+ Message mes = preProcessMessage( msg );
+ if( mes != null ){
synchronized(stateLock){
- receiving = false;
+ receiving = false;
}
return mes;
- }
+ }
}
synchronized (messages) {
@@ -124,10 +124,10 @@
if (closed){
return null;
}
- Message mes = getMessage();
- if (mes != null){
+ Message mes = getMessage();
+ if (mes != null){
return mes;
- }
+ }
cat.debug("SpyMessageConsumer: receive in
messages.wait()");
messages.wait();
}
@@ -138,10 +138,10 @@
}finally{
waitingForMessage = false;
synchronized(stateLock){
- receiving = false;
+ receiving = false;
}
- }
}
+ }
}
@@ -153,24 +153,24 @@
throw new IllegalStateException("The MessageConsumer is
closed");
synchronized(stateLock){
- if(receiving)
+ if(receiving)
throw new JMSException("Another thread is already in receive.");
- if (listening)
- throw new JMSException("A message listener is already
registered");
- receiving = true;
+ if (listening)
+ throw new JMSException("A message listener is already
registered");
+ receiving = true;
}
long endTime = System.currentTimeMillis() + timeOut;
SpyMessage msg = session.connection.receive(subscription, timeOut);
if( msg != null ) {
- Message mes = preProcessMessage( msg );
- if( mes != null ){
+ Message mes = preProcessMessage( msg );
+ if( mes != null ){
synchronized(stateLock){
- receiving = false;
+ receiving = false;
}
return mes;
- }
+ }
}
synchronized (messages) {
@@ -180,10 +180,10 @@
if (closed)
return null;
- Message mes = getMessage();
- if (mes != null) {
+ Message mes = getMessage();
+ if (mes != null) {
return mes;
- }
+ }
long att = endTime -
System.currentTimeMillis();
if (att <= 0) {
@@ -200,7 +200,7 @@
}finally{
waitingForMessage = false;
synchronized(stateLock){
- receiving = false;
+ receiving = false;
}
}
}
@@ -212,19 +212,19 @@
throw new IllegalStateException("The MessageConsumer is
closed");
synchronized(stateLock){
- if(receiving)
+ if(receiving)
throw new JMSException("Another thread is already in receive.");
- if (listening)
- throw new JMSException("A message listener is already
registered");
- receiving = true;
+ if (listening)
+ throw new JMSException("A message listener is already
registered");
+ receiving = true;
}
SpyMessage msg = session.connection.receive(subscription, -1);
synchronized(stateLock){
- receiving = false;
+ receiving = false;
}
if(msg == null)
- return null;
+ return null;
return preProcessMessage( msg );
}
@@ -234,12 +234,12 @@
if (closed)
return;
- if (!sessionConsumer)
- session.removeConsumer(this);
-
closed = true;
- messages.notify();
+ messages.notify();
}
+
+ if (!sessionConsumer)
+ session.removeConsumer(this);
}
public void addMessage(SpyMessage message) throws JMSException {
@@ -254,15 +254,15 @@
//Add a message to the queue
if( subscription.accepts(message) ) {
if(sessionConsumer){
- sessionConsumerProcessMessage(message);
+ sessionConsumerProcessMessage(message);
}else{
- if(waitingForMessage){
+ if(waitingForMessage){
messages.addLast(message);
messages.notifyAll();
- }else{
+ }else{
//unwanted message (due to consumer receive timing out) Nack
it.
session.connection.send(
message.getAcknowledgementRequest(false));
- }
+ }
}
} else {
cat.debug("WARNING: NACK issued. The subscription did not accept the
message");
@@ -271,104 +271,104 @@
}
}
- protected void sessionConsumerProcessMessage(SpyMessage message)throws
JMSException{
+ protected void sessionConsumerProcessMessage(SpyMessage message)throws
JMSException{
message.session = session;
if (message.isOutdated()) {
- cat.debug("SessionQueue: I dropped a message (timeout)");
- message.doAcknowledge();
- return;
+ cat.debug("SessionQueue: I dropped a message (timeout)");
+ message.doAcknowledge();
+ return;
}
//simply pass on to messageListener (if there is one)
MessageListener thisListener;
synchronized(stateLock){
- thisListener = messageListener;
+ thisListener = messageListener;
}
if(thisListener != null){
- Message mes = message;
- if(message instanceof SpyEncapsulatedMessage)
+ Message mes = message;
+ if(message instanceof SpyEncapsulatedMessage)
mes = ((SpyEncapsulatedMessage)message).getMessage();
- thisListener.onMessage(mes);
+ thisListener.onMessage(mes);
}
//ack
if (session.transacted) {
-
session.connection.spyXAResourceManager.ackMessage(session.currentTransactionId,
message);
+
session.connection.spyXAResourceManager.ackMessage(session.currentTransactionId,
message);
} else if (session.acknowledgeMode == session.AUTO_ACKNOWLEDGE ||
session.acknowledgeMode == session.DUPS_OK_ACKNOWLEDGE) {
- message.doAcknowledge();
+ message.doAcknowledge();
}
- }
+ }
- protected boolean isListening(){
+ protected boolean isListening(){
synchronized(stateLock){
- return listening;
+ return listening;
+ }
}
- }
- //Used to facilitate delivery of messages to a message listener.
- public void run(){
+ //Used to facilitate delivery of messages to a message listener.
+ public void run(){
SpyMessage mes = null;
try{
- outer: while(true){
+ outer: while(true){
//get Message
while(mes == null){
- mes = session.connection.receive(subscription,0);
- if(mes == null){
+ mes = session.connection.receive(subscription,0);
+ if(mes == null){
synchronized(messages){
- waitingForMessage = true;
- while(messages.isEmpty() && !closed){
+ waitingForMessage = true;
+ while(messages.isEmpty() && !closed){
try{ messages.wait(); }catch(InterruptedException e){}
- }
- if(closed){
+ }
+ if(closed){
waitingForMessage = false;
break outer;
- }
- mes = (SpyMessage)messages.removeFirst();
- waitingForMessage = false;
- }
- }
- mes.session = session;
- if (mes.isOutdated()) {
+ }
+ mes = (SpyMessage)messages.removeFirst();
+ waitingForMessage = false;
+ }
+ }
+ mes.session = session;
+ if (mes.isOutdated()) {
//Drop message (it has expired)
mes.doAcknowledge();
mes = null;
- }
+ }
}
MessageListener thisListener;
synchronized(stateLock){
- if(!isListening()){
+ if(!isListening()){
//send NACK cause we have closed listener
if(mes != null){
- session.connection.send(
mes.getAcknowledgementRequest(false) );
+ session.connection.send(
mes.getAcknowledgementRequest(false) );
}
//this thread is about to die, so we will need a new one if a
new listener is added
listenerThread = null;
mes = null;
break;
- }
- thisListener = messageListener;
+ }
+ thisListener = messageListener;
}
Message message = mes;
if( mes instanceof SpyEncapsulatedMessage ) {
- message = ((SpyEncapsulatedMessage)mes).getMessage();
+ message = ((SpyEncapsulatedMessage)mes).getMessage();
}
thisListener.onMessage(message);
if (session.transacted) {
-
session.connection.spyXAResourceManager.ackMessage(session.currentTransactionId, mes);
+
session.connection.spyXAResourceManager.ackMessage(session.currentTransactionId, mes);
} else if (session.acknowledgeMode == session.AUTO_ACKNOWLEDGE ||
session.acknowledgeMode == session.DUPS_OK_ACKNOWLEDGE) {
- mes.doAcknowledge();
+ mes.doAcknowledge();
}
mes = null;
- }
+ }
}catch(JMSException e){
- cat.warn("Message consumer closing due to error in listening thread.",e);
- try{
+ cat.warn("Message consumer closing due to error in listening
thread.",e);
+ try{
close();
- }catch(Exception ignore){}
+ }catch(Exception ignore){}
+ }
}
- }
- public Subscription getSubscription() {
+ public Subscription getSubscription() {
return subscription;
}
1.3 +138 -136 jbossmq/src/main/org/jboss/mq/SpyConnectionConsumer.java
Index: SpyConnectionConsumer.java
===================================================================
RCS file: /cvsroot/jboss/jbossmq/src/main/org/jboss/mq/SpyConnectionConsumer.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SpyConnectionConsumer.java 2001/08/13 22:34:45 1.2
+++ SpyConnectionConsumer.java 2001/08/16 04:04:40 1.3
@@ -19,150 +19,152 @@
*
* @author Hiram Chirino ([EMAIL PROTECTED])
*
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class SpyConnectionConsumer implements javax.jms.ConnectionConsumer,
SpyConsumer, Runnable {
- // The connection is the consumer was created with
- Connection connection;
- // The destination this consumer will receive messages from
- Destination destination;
- // The ServerSessionPool that is implemented by the AS
- javax.jms.ServerSessionPool serverSessionPool;
- // The maximum number of messages that a single session will be loaded with.
- int maxMessages;
- // This queue will hold messages until they are dispatched to the
MessageListener
- LinkedList queue = new LinkedList();
- // Is the ConnectionConsumer closed?
- boolean closed = false;
+ // The connection is the consumer was created with
+ Connection connection;
+ // The destination this consumer will receive messages from
+ Destination destination;
+ // The ServerSessionPool that is implemented by the AS
+ javax.jms.ServerSessionPool serverSessionPool;
+ // The maximum number of messages that a single session will be loaded with.
+ int maxMessages;
+ // This queue will hold messages until they are dispatched to the MessageListener
+ LinkedList queue = new LinkedList();
+ // Is the ConnectionConsumer closed?
+ boolean closed = false;
boolean waitingForMessage = false;
- // The subscription info the consumer
- Subscription subscription = new Subscription();
+ // The subscription info the consumer
+ Subscription subscription = new Subscription();
// The "listening" thread that gets messages from destination and queues them for
delivery to sessions
Thread internalThread;
- public void addMessage(SpyMessage mes) throws JMSException {
- cat.debug(""+this+"->addMessage(mes="+mes+")");
- synchronized(queue){
- if( closed ) {
- cat.debug("WARNING: NACK issued. The connection consumer was closed.");
- connection.send( mes.getAcknowledgementRequest(false) );
- return;
- }
-
- if(waitingForMessage){
- queue.addLast(mes);
- queue.notifyAll();
- }else{
- //unwanted message (due to consumer receive timing out?) Nack it.
- connection.send( mes.getAcknowledgementRequest(false));
- }
- }
- }
-
- /**
- * close method comment.
- */
- public void close() throws javax.jms.JMSException {
- synchronized(queue){
- if (closed)
- return;
-
- closed = true;
- queue.notifyAll();
- }
- if(internalThread != null && !internalThread.equals(Thread.currentThread()))
- try{ internalThread.join(); }catch(InterruptedException e){}
- synchronized(queue){
- //nack messages on queue
- while(!queue.isEmpty()){
- SpyMessage message = (SpyMessage) queue.removeFirst();
- connection.send(message.getAcknowledgementRequest(false));
- }
- connection.removeConsumer(this);
- }
- }
-
- /**
- * getServerSessionPool method comment.
- */
- public javax.jms.ServerSessionPool getServerSessionPool() throws
javax.jms.JMSException {
- return serverSessionPool;
- }
-
- public Subscription getSubscription() {
- return subscription;
- }
-
- public String toString() {
- return "SpyConnectionConsumer:"+destination;
- }
+ public void addMessage(SpyMessage mes) throws JMSException {
+ cat.debug(""+this+"->addMessage(mes="+mes+")");
+ synchronized(queue){
+ if( closed ) {
+ cat.debug("WARNING: NACK issued. The connection consumer was closed.");
+ connection.send( mes.getAcknowledgementRequest(false) );
+ return;
+ }
+
+ if(waitingForMessage){
+ queue.addLast(mes);
+ queue.notifyAll();
+ }else{
+ //unwanted message (due to consumer receive timing out?) Nack it.
+ connection.send( mes.getAcknowledgementRequest(false));
+ }
+ }
+ }
+
+ /**
+ * close method comment.
+ */
+ public void close() throws javax.jms.JMSException {
+ synchronized(queue){
+ if (closed)
+ return;
+
+ closed = true;
+ queue.notifyAll();
+ }
+ if(internalThread != null && !internalThread.equals(Thread.currentThread()))
+ try{ internalThread.join(); }catch(InterruptedException e){}
+ synchronized(queue){
+ //nack messages on queue
+ while(!queue.isEmpty()){
+ SpyMessage message = (SpyMessage) queue.removeFirst();
+ connection.send(message.getAcknowledgementRequest(false));
+ }
+ connection.removeConsumer(this);
+ }
+ }
+
+ /**
+ * getServerSessionPool method comment.
+ */
+ public javax.jms.ServerSessionPool getServerSessionPool() throws
javax.jms.JMSException {
+ return serverSessionPool;
+ }
+
+ public Subscription getSubscription() {
+ return subscription;
+ }
+
+ public String toString() {
+ return "SpyConnectionConsumer:"+destination;
+ }
//Used to facilitate delivery of messages to sessions from server session pool.
public void run(){
- SpyMessage mes = null;
- try{
- outer: while(true){
- //get Message
- while(mes == null){
- mes = connection.receive(subscription,0);
- if(mes == null){
- synchronized(queue){
- waitingForMessage = true;
- while(queue.isEmpty() && !closed){
- try{ queue.wait(); }catch(InterruptedException e){}
- }
- if(closed){
- waitingForMessage = false;
- break outer;
- }
- mes = (SpyMessage)queue.removeFirst();
- waitingForMessage = false;
- }
- }
- }
- ServerSession serverSession = serverSessionPool.getServerSession();
- SpySession spySession = (SpySession) serverSession.getSession();
-
- if (spySession.sessionConsumer == null) {
- cat.debug(""+this+" Session did not have a set MessageListner");
- }else{
- spySession.sessionConsumer.subscription = subscription;
- }
-
- spySession.addMessage(mes);
-
- cat.debug(""+this+" Starting the ServerSession.");
- serverSession.start();
- mes = null;
- }
- }catch(JMSException e){
- cat.warn("Connection consumer closing due to error in listening thread.",e);
- try{
- close();
- }catch(Exception ignore){}
- }
- }
-
- static org.apache.log4j.Category cat =
org.apache.log4j.Category.getInstance(SpyConnectionConsumer.class);
-
- /**
- * SpyConnectionConsumer constructor comment.
- */
- public SpyConnectionConsumer(Connection connection, Destination destination,
String messageSelector, ServerSessionPool serverSessionPool, int maxMessages)
- throws JMSException {
-
- this.connection = connection;
- this.destination = destination;
- this.serverSessionPool = serverSessionPool;
- this.maxMessages = maxMessages;
-
- subscription.destination = (SpyDestination)destination;
- subscription.messageSelector = messageSelector;
- subscription.noLocal = false;
-
- connection.addConsumer(this);
- internalThread = new Thread(this,"Connection Consumer for dest "+destination);
- internalThread.start();
- }
+ SpyMessage mes = null;
+ try{
+ outer: while(true){
+ //get Message
+ while(mes == null){
+ mes = connection.receive(subscription,0);
+ if(mes == null){
+ synchronized(queue){
+ waitingForMessage = true;
+ while(queue.isEmpty() && !closed){
+ try{ queue.wait(); }catch(InterruptedException e){}
+ }
+ if(closed){
+ waitingForMessage = false;
+ break outer;
+ }
+ mes = (SpyMessage)queue.removeFirst();
+ waitingForMessage = false;
+ }
+ }
+ }
+ ServerSession serverSession = serverSessionPool.getServerSession();
+ SpySession spySession = (SpySession) serverSession.getSession();
+
+ if (spySession.sessionConsumer == null) {
+ cat.debug(""+this+" Session did not have a set MessageListner");
+ }else{
+ spySession.sessionConsumer.subscription = subscription;
+ }
+
+ spySession.addMessage(mes);
+
+ cat.debug(""+this+" Starting the ServerSession.");
+ serverSession.start();
+ mes = null;
+ }
+ }catch(JMSException e){
+ cat.warn("Connection consumer closing due to error in listening thread.",e);
+ try{
+ if(mes != null)
+ connection.send(mes.getAcknowledgementRequest(false));
+ close();
+ }catch(Exception ignore){}
+ }
+ }
+
+ static org.apache.log4j.Category cat =
org.apache.log4j.Category.getInstance(SpyConnectionConsumer.class);
+
+ /**
+ * SpyConnectionConsumer constructor comment.
+ */
+ public SpyConnectionConsumer(Connection connection, Destination destination,
String messageSelector, ServerSessionPool serverSessionPool, int maxMessages)
+ throws JMSException {
+
+ this.connection = connection;
+ this.destination = destination;
+ this.serverSessionPool = serverSessionPool;
+ this.maxMessages = maxMessages;
+
+ subscription.destination = (SpyDestination)destination;
+ subscription.messageSelector = messageSelector;
+ subscription.noLocal = false;
+
+ connection.addConsumer(this);
+ internalThread = new Thread(this,"Connection Consumer for dest "+destination);
+ internalThread.start();
+ }
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development