package org.mom4j.messaging;
import org.mom4j.messaging.select.Element;
import java.io.IOException;
import javax.jms.JMSException;
import javax.jms.Message;
public class PubSubFilter implements Filter {
private Console console;
public PubSubFilter(Console c) {
this.console = c;
}
public void send(String sessionId,
String destination,
Message msg,
boolean transacted)
throws IOException,
JMSException
{
String[] cs = this.console.getConsumerIds(destination);
if(cs.length > 0) {
String[] destinations = new String[cs.length];
for(int i = 0; i < cs.length; i++) {
destinations[i] = destination + "-" + cs[i];
}
this.console.sendInternal(sessionId, destinations, msg);
} //else: no subscribers: no need to write the message ...
if(!transacted) {
//This is for non-transacted sessions
this.console.commit(sessionId);
}
}
public Message receive(String sessionId,
String destination,
String consumerId,
boolean transacted)
throws IOException,
JMSException
{
Element e = this.console.getMessageSelector(destination, consumerId);
Message ret = null;
String dest = destination + "-" + consumerId;
if(e != null) {
while((ret = this.console.receiveInternal(sessionId, dest)) != null) {
if(e.match(ret)) {
break;
}
}
} else {
ret = this.console.receiveInternal(sessionId, dest);
}
if(!transacted) {
//This is for non-transacted sessions
this.console.commit(sessionId);
}
return ret;
}
}