use of com.sun.messaging.jmq.jmsserver.core.ConsumerUID in project openmq by eclipse-ee4j.
the class JMSServiceImpl method browseMessages.
@Override
public JMSPacket[] browseMessages(long connectionId, long sessionId, long consumerId) throws JMSServiceException {
IMQConnection cxn;
HashMap props = new HashMap();
ConsumerUID uid;
// Session session;
JMSPacket[] msgs = null;
cxn = checkConnectionId(connectionId, "browseMessages");
// session = checkSessionId(sessionId, "browseMessages");
checkSessionId(sessionId, "browseMessages");
uid = new ConsumerUID(consumerId);
if (queueBrowseList.containsKey(uid)) {
QueueBrowserInfo qbi = (QueueBrowserInfo) queueBrowseList.get(uid);
try {
Destination dest = qbi.dest;
String selector = qbi.selector;
com.sun.messaging.jmq.jmsserver.core.Destination[] ds = null;
com.sun.messaging.jmq.jmsserver.core.Destination d = null;
ds = Globals.getDestinationList().getDestination(cxn.getPartitionedStore(), dest.getName(), (dest.getType() == Destination.Type.QUEUE));
d = ds[0];
if (d == null) {
String errStr = "browseMessages: destination not found. Connection ID:" + connectionId + ", Session ID: " + sessionId + ", Consumer ID: " + consumerId + "destination: " + dest.toString();
logger.log(Logger.ERROR, errStr);
props.put("JMQStatus", JMSServiceReply.Status.NOT_FOUND);
throw new JMSServiceException(errStr, props);
}
ArrayList msgIds = protocol.browseQueue(d, selector, cxn, cxn.getAccessController().isAccessControlEnabled());
if (msgIds != null) {
int numMsgs = msgIds.size();
if (numMsgs == 0) {
return (null);
}
msgs = new JMSPacket[numMsgs];
for (int i = 0; i < numMsgs; ++i) {
PacketReference pr = DestinationList.get(null, (SysMessageID) msgIds.get(i));
msgs[i] = pr.getPacket();
}
}
} catch (Exception e) {
String errStr = "browseMessages: Browse queue failed. Connection ID: " + connectionId + ", session ID: " + sessionId + ", consumer ID: " + consumerId;
logger.logStack(Logger.ERROR, errStr, e);
if (e instanceof SelectorFormatException) {
props.put("JMQStatus", JMSServiceReply.Status.BAD_REQUEST);
} else {
props.put("JMQStatus", getErrorReplyStatus(e));
}
if (e instanceof BrokerException) {
String ecode = ((BrokerException) e).getErrorCode();
if (ecode != null) {
props.put(JMSPacketProperties.JMQErrorCode, ecode);
}
}
throw new JMSServiceException(errStr, e, props);
}
} else {
String errStr = "browseMessages: consumer ID not found. Connection ID:" + connectionId + ", Session ID: " + sessionId + ", Consumer ID: " + consumerId;
logger.log(Logger.ERROR, errStr);
props.put("JMQStatus", JMSServiceReply.Status.NOT_FOUND);
throw new JMSServiceException(errStr, props);
}
return (msgs);
}
use of com.sun.messaging.jmq.jmsserver.core.ConsumerUID in project openmq by eclipse-ee4j.
the class JMSServiceImpl method fetchMessage.
/**
* Fetch a message from the broker.
*
* @param connectionId The Id of the connection
* @param sessionId The Id of the session
* @param consumerId The Id of the consumer for which to fetch the message
* @param timeout The maximum time to wait (in milliseconds) for a message to be available before returning.<br>
* Note that the method must return immediately if there is a message available for this consumerId at the time the call
* is made.<br>
* <UL>
* <LI>When timeout is positive, the call must wait for a maximum of the specificied number of milliseconds before
* returning. If a message is available before the timeout expires, the method returns with the message as soon as it is
* available.</LI>
* <LI>When timeout is 0, the call must block until a message is available to return or until the session is stopped.
* </LI>
* <LI>When the timeout is negative (less than 0), the call must return immediately, with a message if one is available
* or with a null, if a message is not available immediately.</LI>
* </UL>
* @param acknowledge If this is set to {@code true} then it implies that the caller is asking for the message to be
* <b>acknowledged</b> before the method returns. If this operation is part of a transaction, the {@code transactionId}
* parameter will be non-zero.
* @param transactionId If non-zero, this is the transactionId in which to acknowledge the message being returned if the
* {@code acknowledge} parameter is set to {@code true}.
*
* @return The JMSPacket which contains the message being returned.
*
* @throws JMSServiceException if broker encounters an error. {@link JMSServiceReply.Status} contains the reason for the
* error.
*/
@Override
public JMSPacket fetchMessage(long connectionId, long sessionId, long consumerId, long timeout, boolean acknowledge, long transactionId) throws JMSServiceException {
JMSPacket msg = null;
IMQConnection cxn;
Session session;
cxn = checkConnectionId(connectionId, "fetchMessage");
session = checkSessionId(sessionId, "fetchMessage");
SessionListener slistener = getListener(session.getSessionUID());
ConsumerUID conUID;
try {
conUID = new ConsumerUID(consumerId);
msg = slistener.getNextConsumerPacket(conUID, timeout);
if ((msg != null) && acknowledge) {
TransactionUID txnUID = null;
if (transactionId != 0) {
txnUID = new TransactionUID(transactionId);
}
SysMessageID[] ids = new SysMessageID[1];
ids[0] = ((Packet) msg).getSysMessageID();
ConsumerUID[] cids = new ConsumerUID[1];
cids[0] = conUID;
Globals.getProtocol().acknowledge(cxn, txnUID, false, AckHandler.ACKNOWLEDGE_REQUEST, null, null, 0, ids, cids);
}
} catch (Exception e) {
HashMap props = new HashMap();
String errStr = "fetchMessage: Fetch Message failed. Connection ID: " + connectionId + ", session ID: " + sessionId + ", consumer ID: " + consumerId;
logger.logStack(Logger.ERROR, errStr, e);
props.put("JMQStatus", JMSServiceReply.Status.ERROR);
throw new JMSServiceException(errStr, e, props);
}
return (msg);
}
use of com.sun.messaging.jmq.jmsserver.core.ConsumerUID in project openmq by eclipse-ee4j.
the class JMSServiceImpl method addBrowser.
@Override
public JMSServiceReply addBrowser(long connectionId, long sessionId, Destination dest, String selector) throws JMSServiceException {
JMSServiceReply reply;
// IMQConnection cxn;
HashMap props = new HashMap();
ConsumerUID uid;
// Session session;
// cxn = checkConnectionId(connectionId, "addBrowser");
checkConnectionId(connectionId, "addBrowser");
// session = checkSessionId(sessionId, "addBrowser");
checkSessionId(sessionId, "addBrowser");
try {
Selector.compile(selector);
} catch (SelectorFormatException sfe) {
String errStr = "addBrowser: Add browser failed. Connection ID: " + connectionId + ", session ID: " + sessionId + ", destination: " + dest + ", selector: " + selector;
logger.logStack(Logger.ERROR, errStr, sfe);
props.put("JMQStatus", JMSServiceReply.Status.BAD_REQUEST);
throw new JMSServiceException(errStr, sfe, props);
}
uid = new ConsumerUID();
queueBrowseList.put(uid, new QueueBrowserInfo(dest, selector));
props.put("JMQStatus", JMSServiceReply.Status.OK);
props.put("JMQConsumerID", uid.longValue());
reply = new JMSServiceReply(props);
return (reply);
}
use of com.sun.messaging.jmq.jmsserver.core.ConsumerUID in project openmq by eclipse-ee4j.
the class JMSServiceImpl method deleteConsumer.
/**
* Delete a consumer.
* <p>
* If this operation is deleting a consumer that is a durable subscription, then the durableName <b>and</b> the clientId
* must be non-null. In addition, the clientId must match the clientId that is currently set on the connection
* identified by connectionId.
*
* @param connectionId The Id of the connection
* @param sessionId The Id of the session
* @param consumerId The Id of the consumer to delete
* @param lastMessageSeen The last message received by this consumer which has been seen by the application. Set to null
* if deleting a durable subscription.
* @param durableName The name of the durable subscription to remove if the consumer is unsubscribing.
* @param clientId The clientId of the connection
*
* @return The JMSServiceReply of the request to delete a consumer
*
* @throws JMSServiceException if the Status returned for the deleteConsumer method is not
* {@link JMSServiceReply.Status#OK}
*
* @see JMSServiceReply.Status#FORBIDDEN
* @see JMSServiceReply.Status#NOT_FOUND
* @see JMSServiceReply.Status#PRECONDITION_FAILED
* @see JMSServiceReply.Status#CONFLICT
* @see JMSServiceReply.Status#ERROR
*/
@Override
public JMSServiceReply deleteConsumer(long connectionId, long sessionId, long consumerId, SysMessageID lastMessageSeen, boolean lastMessageSeenInTransaction, String durableName, String clientId) throws JMSServiceException {
JMSServiceReply reply;
IMQConnection cxn;
Session session;
HashMap props = new HashMap();
cxn = checkConnectionId(connectionId, "deleteConsumer");
session = checkSessionId(sessionId, "deleteConsumer");
try {
if (durableName == null) {
ConsumerUID conUID = new ConsumerUID(consumerId);
protocol.destroyConsumer(conUID, session, lastMessageSeen, lastMessageSeenInTransaction, cxn);
} else {
protocol.unsubscribe(durableName, clientId, cxn);
}
} catch (Exception e) {
String errStr = "deleteConsumer: Delete consumer failed. Connection ID: " + connectionId + ", session ID: " + sessionId + ", consumer ID: " + consumerId + ", durable name: " + durableName + ", client ID: " + clientId;
logger.logStack(Logger.ERROR, errStr, e);
props.put("JMQStatus", getErrorReplyStatus(e));
throw new JMSServiceException(errStr, e, props);
}
props.put("JMQStatus", JMSServiceReply.Status.OK);
reply = new JMSServiceReply(props);
return (reply);
}
use of com.sun.messaging.jmq.jmsserver.core.ConsumerUID in project openmq by eclipse-ee4j.
the class SessionListener method process.
/**
* method which handles delivering messages
*/
public void process() {
if (sync) {
// there doesnt need to be an external thread
throw new RuntimeException("Cannot invoke SessionListener.process() when in synchronous receiving mode");
}
synchronized (sessionLock) {
if (destroyed) {
valid = false;
sessionLock.notifyAll();
return;
}
valid = true;
}
while (valid) {
// if we are not busy, wait for the eventListener to wake us up
while (valid && (!session.isBusy() || stopped)) {
// get the lock
synchronized (sessionLock) {
islocked = true;
sessionLock.notifyAll();
// instead check the sessionLockNotify flag
if (sessionLockNotify || !valid || stopped) {
sessionLockNotify = false;
continue;
}
try {
sessionLock.wait();
} catch (Exception ex) {
Globals.getLogger().log(Logger.DEBUGHIGH, "Exception in sessionlock wait", ex);
}
}
}
if (!valid) {
continue;
}
synchronized (sessionLock) {
// we dont care if we are about to notify
sessionLockNotify = false;
islocked = false;
}
if (session.isBusy() && !stopped && valid) {
// cool, we have something to do
Packet p = new Packet();
// retrieve the next packet and its ConsumerUID
ConsumerUID uid = session.fillNextPacket(p);
if (uid == null) {
// weird, something went wrong, try again
continue;
}
// Get the consumer object
Consumer con = (Consumer) consumers.get(uid);
try {
JMSAck ack = null;
// call the deliver method
ack = con.deliver(p);
if (ack != null) {
long transactionId = ack.getTransactionId(), consumerId = ack.getConsumerId();
SysMessageID sysMsgId = ack.getSysMessageID();
TransactionUID txnUID = null;
ConsumerUID conUID = null;
if (transactionId != 0) {
txnUID = new TransactionUID(transactionId);
}
if (consumerId != 0) {
conUID = new ConsumerUID(consumerId);
}
IMQConnection cxn = parent.checkConnectionId(ack.getConnectionId(), "Listener Thread");
SysMessageID[] ids = new SysMessageID[1];
// ids[0] = sysMsgId;
// ids[0] = ((Packet)p).getSysMessageID();
ids[0] = sysMsgId;
ConsumerUID[] cids = new ConsumerUID[1];
cids[0] = conUID;
Globals.getProtocol().acknowledge(cxn, txnUID, false, AckHandler.ACKNOWLEDGE_REQUEST, null, null, 0, ids, cids);
}
} catch (Exception ex) {
if (ex instanceof ConsumerClosedNoDeliveryException) {
if (parent.getDEBUG()) {
Globals.getLogger().logStack(Logger.INFO, "DirectConsumer " + con + " is closed, message " + p.getSysMessageID() + " can not be deliverd", ex);
}
} else {
// I have no idea what the exception might mean so just
// log it and go on
Globals.getLogger().logStack(Logger.ERROR, Globals.getBrokerResources().getKString(BrokerResources.X_CANNOT_DELIVER_MESSAGE_TO_CONSUMER, p.getSysMessageID(), uid + " DirectConsumer[" + con + "]"), ex);
}
}
}
}
}
Aggregations