use of com.sun.messaging.jmq.jmsserver.core.Producer in project openmq by eclipse-ee4j.
the class ProtocolImpl method processMessage.
/**
* route, store and forward a message
*/
@Override
public void processMessage(IMQConnection con, Packet msg) throws BrokerException, SelectorFormatException, IOException {
DataHandler handler = (DataHandler) pr.getHandler(PacketType.MESSAGE);
Destination d = null;
PacketReference ref = null;
Set s = null;
boolean route = false;
boolean isadmin = con.isAdminConnection();
List<MessageDeliveryTimeInfo> deliveryDelayReadyList = new ArrayList<>();
try {
Destination[] ds = DL.getDestination(con.getPartitionedStore(), msg.getDestination(), msg.getIsQueue());
d = ds[0];
if (d == null) {
throw new BrokerException("Unknown Destination:" + msg.getDestination());
}
Producer pausedProducer = handler.checkFlow(msg, con);
boolean transacted = (msg.getTransactionID() != 0);
if (DEBUG) {
logger.log(Logger.INFO, "ProtocolImpl.PROCESS MESSAGE[" + msg + "]TID=" + msg.getTransactionID() + " to destination " + d + ", on conn=@" + con.hashCode() + "[" + con.getConnectionUID() + ", " + con + "]");
}
// OK generate a ref. This checks message size and
// will be needed for later operations
ref = handler.createReference(msg, d.getDestinationUID(), con, isadmin);
// dont bother calling route if there are no messages
//
// to improve performance, we route and later forward
route = handler.queueMessage(d, ref, transacted);
s = handler.routeMessage(con.getPartitionedStore(), transacted, ref, route, d, deliveryDelayReadyList);
// handle producer flow control
handler.pauseProducer(d, pausedProducer, con);
} catch (BrokerException ex) {
int status = ex.getStatusCode();
if (status == Status.ERROR && ref != null && d != null) {
handler.cleanupOnError(d, ref);
}
// rethrow
throw ex;
}
if (route && s != null) {
try {
handler.forwardMessage(d, ref, s);
} catch (Exception e) {
Object[] emsg = { ref, d.getDestinationUID(), s };
logger.logStack(Logger.WARNING, Globals.getBrokerResources().getKString(BrokerResources.X_ROUTE_PRODUCED_MSG_FAIL, emsg), e);
}
}
if (deliveryDelayReadyList.size() > 0) {
MessageDeliveryTimeInfo di = null;
Iterator<MessageDeliveryTimeInfo> itr = deliveryDelayReadyList.iterator();
while (itr.hasNext()) {
di = itr.next();
di.setDeliveryReady();
}
}
}
use of com.sun.messaging.jmq.jmsserver.core.Producer in project openmq by eclipse-ee4j.
the class DataHandler method handle.
/**
* Method to handle normal/admin data messages
*/
protected boolean handle(IMQConnection con, Packet msg, boolean isadmin) throws BrokerException {
// used for fault injection
Hashtable props = null;
if (!isadmin && fi.FAULT_INJECTION) {
// for fault injection
msgProcessCnt++;
try {
props = msg.getProperties();
} catch (Exception ex) {
props = new Properties();
}
} else {
msgProcessCnt = 0;
}
boolean ack = msg.getSendAcknowledge();
long cid = msg.getConsumerID();
String refid = (((IMQBasicConnection) con).getDumpPacket() || ((IMQBasicConnection) con).getDumpOutPacket()) ? msg.getSysMessageID().toString() : "";
boolean isIndemp = msg.getIndempotent();
String reason = null;
List failedrefs = null;
int status = Status.OK;
HashMap routedSet = null;
List<MessageDeliveryTimeInfo> deliveryDelayReadyList = new ArrayList<>();
boolean route = false;
Producer pausedProducer = null;
boolean transacted = false;
try {
pausedProducer = checkFlow(msg, con);
transacted = (msg.getTransactionID() != 0);
// OK .. handle Fault Injection
if (!isadmin && fi.FAULT_INJECTION) {
Map m = new HashMap();
if (props != null) {
m.putAll(props);
}
m.put("mqMsgCount", Integer.valueOf(msgProcessCnt));
m.put("mqIsTransacted", Boolean.valueOf(transacted));
fi.checkFaultAndExit(FaultInjection.FAULT_SEND_MSG_1, m, 2, false);
if (fi.checkFault(FaultInjection.FAULT_SEND_MSG_1_EXCEPTION, m)) {
fi.unsetFault(FaultInjection.FAULT_SEND_MSG_1_EXCEPTION);
throw new BrokerException("FAULT INJECTION: " + FaultInjection.FAULT_SEND_MSG_1_EXCEPTION);
}
if (fi.checkFaultAndSleep(FaultInjection.FAULT_SEND_MSG_1_SLEEP, m, true)) {
fi.unsetFault(FaultInjection.FAULT_SEND_MSG_1_SLEEP);
}
if (fi.checkFaultAndSleep(FaultInjection.FAULT_SEND_MSG_1_SLEEP_EXCEPTION, m, true)) {
fi.unsetFault(FaultInjection.FAULT_SEND_MSG_1_SLEEP_EXCEPTION);
throw new BrokerException("FAULT INJECTION: " + FaultInjection.FAULT_SEND_MSG_1_SLEEP_EXCEPTION);
}
}
DestinationUID realduid = DestinationUID.getUID(msg.getDestination(), msg.getIsQueue());
if (DEBUG) {
logger.log(Logger.INFO, "DataHandler:Received JMS Message[" + msg.toString() + " : " + realduid + "]TID=" + msg.getTransactionID() + " on connection " + con + ", isadmin=" + isadmin);
}
// get the list of "real" destination UIDs (this will be one if the
// destination if not a wildcard and 0 or more if it is
List[] dds = DL.findMatchingIDs(con.getPartitionedStore(), realduid);
List duids = dds[0];
boolean packetUsed = false;
if (duids.size() == 0) {
// nothing to do
route = false;
} else {
Iterator itr = duids.iterator();
while (itr.hasNext()) {
PacketReference ref = null;
Exception lastthr = null;
boolean isLast = false;
DestinationUID duid = (DestinationUID) itr.next();
isLast = !itr.hasNext();
Destination[] ds = DL.getDestination(con.getPartitionedStore(), duid);
Destination d = ds[0];
try {
if (d == null) {
throw new BrokerException("Unknown Destination:" + msg.getDestination());
}
if (realduid.isWildcard() && d.isTemporary()) {
logger.log(Logger.DEBUG, "L10N-XXX: Wildcard production with destination name of " + realduid + " to temporary destination " + d.getUniqueName() + " is not supported, ignoring");
continue;
}
if (realduid.isWildcard() && d.isInternal()) {
logger.log(Logger.DEBUG, "L10N-XXX: Wildcard production with destination name of " + realduid + " to internal destination " + d.getUniqueName() + " is not supported, ignoring");
continue;
}
if (realduid.isWildcard() && d.isDMQ()) {
logger.log(Logger.DEBUG, "L10N-XXX: Wildcard production with destination name of " + realduid + " to the DeadMessageQueue" + d.getUniqueName() + " is not supported, ignoring");
continue;
}
if (pausedProducer != null) {
pauseProducer(d, duid, pausedProducer, con);
pausedProducer = null;
}
if (packetUsed) {
// create a new Packet for the message
// we need a new sysmsgid with it
Packet newp = new Packet();
newp.fill(msg);
newp.generateSequenceNumber(true);
newp.generateTimestamp(true);
newp.prepareToSend();
newp.generateSequenceNumber(false);
newp.generateTimestamp(false);
msg = newp;
}
packetUsed = true;
// OK generate a ref. This checks message size and
// will be needed for later operations
ref = createReference(msg, duid, con, isadmin);
// dont bother calling route if there are no messages
//
// to improve performance, we route and later forward
route |= queueMessage(d, ref, transacted);
// ok ...
if (isLast && route && ack && !ref.isPersistent()) {
sendAcknowledge(refid, cid, status, con, reason, props, transacted);
ack = false;
}
Set s = routeMessage(con.getPartitionedStore(), transacted, ref, route, d, deliveryDelayReadyList);
if (s != null && !s.isEmpty()) {
if (routedSet == null) {
routedSet = new HashMap();
}
routedSet.put(ref, s);
}
// handle producer flow control
pauseProducer(d, duid, pausedProducer, con);
} catch (Exception ex) {
if (ref != null) {
if (failedrefs == null) {
failedrefs = new ArrayList();
}
failedrefs.add(ref);
}
lastthr = ex;
logger.log(Logger.DEBUG, BrokerResources.W_MESSAGE_STORE_FAILED, con.toString(), ex);
} finally {
if (pausedProducer != null) {
pauseProducer(d, duid, pausedProducer, con);
pausedProducer = null;
}
if (isLast && lastthr != null) {
throw lastthr;
}
}
}
// while
}
} catch (BrokerException ex) {
// dont log on dups if indemponent
int loglevel = (isIndemp && ex.getStatusCode() == Status.NOT_MODIFIED) ? Logger.DEBUG : Logger.WARNING;
logger.log(loglevel, BrokerResources.W_MESSAGE_STORE_FAILED, con.toString(), ex);
reason = ex.getMessage();
// LKS - we may want an improved error message in the wildcard case
status = ex.getStatusCode();
} catch (IOException ex) {
logger.log(Logger.WARNING, BrokerResources.W_MESSAGE_STORE_FAILED, con.toString(), ex);
reason = ex.getMessage();
status = Status.ERROR;
} catch (SecurityException ex) {
logger.log(Logger.WARNING, BrokerResources.W_MESSAGE_STORE_FAILED, con.toString(), ex);
reason = ex.getMessage();
status = Status.FORBIDDEN;
} catch (OutOfMemoryError err) {
logger.logStack(Logger.WARNING, BrokerResources.W_MESSAGE_STORE_FAILED, con.toString() + ":" + msg.getPacketSize(), err);
reason = err.getMessage();
status = Status.ERROR;
} catch (Exception ex) {
logger.logStack(Logger.WARNING, BrokerResources.W_MESSAGE_STORE_FAILED, con.toString(), ex);
reason = ex.getMessage();
status = Status.ERROR;
}
if (status == Status.ERROR && failedrefs != null) {
// make sure we remove the message
//
// NOTE: we only want to remove the last failure (its too late
// for the rest). In the non-wildcard case, this will be the
// only entry. In the wildcard cause, it will be the one that had an issue
Iterator itr = failedrefs.iterator();
while (itr.hasNext()) {
PacketReference ref = (PacketReference) itr.next();
Destination[] ds = DL.getDestination(con.getPartitionedStore(), ref.getDestinationUID());
Destination d = ds[0];
if (d != null) {
cleanupOnError(d, ref);
}
}
}
if (ack) {
sendAcknowledge(refid, cid, status, con, reason, props, transacted);
}
if (route && routedSet != null) {
Iterator<Map.Entry> itr = routedSet.entrySet().iterator();
Map.Entry pair = null;
while (itr.hasNext()) {
pair = itr.next();
PacketReference pktref = (PacketReference) pair.getKey();
DestinationUID duid = pktref.getDestinationUID();
Set s = (Set) pair.getValue();
Destination[] ds = DL.getDestination(con.getPartitionedStore(), duid);
Destination dest = ds[0];
if (dest == null) {
Object[] emsg = { pktref, s, duid };
logger.log(logger.WARNING, br.getKString(br.W_ROUTE_PRODUCED_MSG_DEST_NOT_FOUND, emsg));
continue;
}
try {
forwardMessage(dest, pktref, s);
} catch (Exception e) {
Object[] emsg = { pktref, duid, s };
logger.logStack(logger.WARNING, br.getKString(br.X_ROUTE_PRODUCED_MSG_FAIL, emsg), e);
}
}
}
if (deliveryDelayReadyList.size() > 0) {
MessageDeliveryTimeInfo di = null;
Iterator<MessageDeliveryTimeInfo> itr = deliveryDelayReadyList.iterator();
while (itr.hasNext()) {
di = itr.next();
di.setDeliveryReady();
}
}
// someone else will free
return isadmin;
}
use of com.sun.messaging.jmq.jmsserver.core.Producer in project openmq by eclipse-ee4j.
the class DataHandler method checkFlow.
public Producer checkFlow(Packet msg, IMQConnection con) {
Producer pausedProducer = null;
// check and clearout the F bit (before anything)
if (msg.getFlowPaused()) {
con.flowPaused(0);
msg.setFlowPaused(false);
}
long pid = msg.getProducerID();
ProducerUID puid = new ProducerUID(pid);
Producer p = (Producer) Producer.getProducer(puid);
if (p != null) {
// increment counter
p.addMsg();
}
// see if we need to resume flow
if (msg.getConsumerFlow()) {
pausedProducer = p;
if (pausedProducer == null) {
logger.log(Logger.WARNING, "Unknown ProducerUID " + puid);
} else if (pausedProducer.getConnectionUID() != con.getConnectionUID()) {
logger.log(Logger.WARNING, "Producer " + pausedProducer + " not on this connection " + con.getConnectionUID());
}
msg.setConsumerFlow(false);
}
return pausedProducer;
}
use of com.sun.messaging.jmq.jmsserver.core.Producer in project openmq by eclipse-ee4j.
the class ProducerHandler method handle.
/**
* Method to handle Producers
*/
@Override
public boolean handle(IMQConnection con, Packet msg) throws BrokerException {
Packet reply = new Packet(con.useDirectBuffers());
reply.setPacketType(msg.getPacketType() + 1);
reply.setConsumerID(msg.getConsumerID());
boolean isIndemp = msg.getIndempotent();
int status = Status.OK;
String reason = null;
Hashtable props = null;
try {
props = msg.getProperties();
} catch (Exception ex) {
throw new RuntimeException("Can not load props", ex);
}
Hashtable returnprop = new Hashtable();
Destination d = null;
try {
if (msg.getPacketType() == PacketType.ADD_PRODUCER) {
String dest = (String) props.get("JMQDestination");
Integer type = (Integer) props.get("JMQDestType");
if (!con.isAdminConnection() && MemoryGlobals.getMEM_DISALLOW_PRODUCERS()) {
status = Status.ERROR;
reason = "Low memory";
logger.log(Logger.WARNING, BrokerResources.W_LOW_MEM_REJECT_PRODUCER);
throw new BrokerException(reason, status);
}
Long lsessionid = (Long) props.get("JMQSessionID");
if (lsessionid != null) {
// 3.5 protocol
SessionUID sessionID = new SessionUID(lsessionid.longValue());
// single threaded .. we dont have to worry about
// someone else creating it
Session session = con.getSession(sessionID);
if (session == null) {
throw new BrokerException("Internal Error: client sent " + "invalid sessionUID w/ ADD_PRODUCER " + sessionID + " session does not exist");
}
}
Destination[] ds = null;
DestinationUID duid = null;
if (dest != null && !DestinationUID.isWildcard(dest) && type != null) {
while (true) {
ds = DL.getDestination(con.getPartitionedStore(), dest, type.intValue(), true, !con.isAdminConnection());
d = ds[0];
if (d != null) {
try {
d.incrementRefCount();
} catch (BrokerException ex) {
// try again
continue;
} catch (IllegalStateException ex) {
throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_SHUTTING_DOWN_BROKER), BrokerResources.X_SHUTTING_DOWN_BROKER, ex, Status.ERROR);
}
}
// got a lock on the dest
break;
}
if (d == null) {
logger.log(Logger.DEBUG, "Unable to add " + "producer to " + dest + " :" + DestType.toString(type.intValue()) + " destination can not be autocreated ");
reason = "can not create destination";
status = Status.NOT_FOUND;
throw new BrokerException(reason, status);
}
duid = d.getDestinationUID();
} else if (dest == null || type == null) {
reason = "no destination passed [dest,type] = [" + dest + "," + type + "]";
status = Status.ERROR;
throw new BrokerException(reason, status);
} else {
duid = DestinationUID.getUID(dest, DestType.isQueue(type.intValue()));
}
String info = msg.getSysMessageID().toString();
Producer p = addProducer(duid, con, info, isIndemp);
ProducerUID pid = p.getProducerUID();
assert pid != null;
// LKS - XXX - REVISIT - WHAT ABOUT FLOW CONTROL
boolean active = d == null || d.isProducerActive(pid);
returnprop.put("JMQProducerID", Long.valueOf(pid.longValue()));
returnprop.put("JMQDestinationID", duid.toString());
if (d == null) {
returnprop.put("JMQBytes", Long.valueOf(-1));
returnprop.put("JMQSize", Integer.valueOf(-1));
} else if (active) {
returnprop.put("JMQBytes", Long.valueOf(d.getBytesProducerFlow()));
returnprop.put("JMQSize", Integer.valueOf(d.getSizeProducerFlow()));
} else {
returnprop.put("JMQBytes", Long.valueOf(0));
returnprop.put("JMQSize", Integer.valueOf(0));
}
} else {
assert msg.getPacketType() == PacketType.DELETE_PRODUCER;
Long pid_l = (Long) props.get("JMQProducerID");
ProducerUID pid = new ProducerUID(pid_l == null ? 0 : pid_l.longValue());
removeProducer(pid, isIndemp, con, "Producer closed requested:\n\tconnection: " + con.getConnectionUID() + "\n\tproducerID: " + pid + "\n\trequest sysmsgid message: " + msg.getSysMessageID());
}
} catch (BrokerException ex) {
status = ex.getStatusCode();
reason = ex.getMessage();
logger.log(Logger.INFO, reason);
} catch (Exception ex) {
logger.logStack(Logger.INFO, BrokerResources.E_INTERNAL_BROKER_ERROR, "producer message ", ex);
reason = ex.getMessage();
status = Status.ERROR;
} finally {
if (d != null) {
d.decrementRefCount();
}
}
returnprop.put("JMQStatus", Integer.valueOf(status));
if (reason != null) {
returnprop.put("JMQReason", reason);
}
if (((IMQBasicConnection) con).getDumpPacket() || ((IMQBasicConnection) con).getDumpOutPacket()) {
returnprop.put("JMQReqID", msg.getSysMessageID().toString());
}
reply.setProperties(returnprop);
con.sendControlMessage(reply);
return true;
}
use of com.sun.messaging.jmq.jmsserver.core.Producer in project openmq by eclipse-ee4j.
the class ProducerHandler method addProducer.
public Producer addProducer(DestinationUID duid, IMQConnection con, String id, boolean isIndemp) throws BrokerException {
Producer p = null;
if (isIndemp) {
p = (Producer) Producer.getProducer(id);
}
if (p == null) {
p = Producer.createProducer(duid, con.getConnectionUID(), id, con.getPartitionedStore());
assert p != null;
con.addProducer(p);
// Add to all destinations
List[] ll = DL.findMatchingIDs(con.getPartitionedStore(), duid);
List l = ll[0];
Iterator itr = l.iterator();
DestinationUID realuid = null;
Destination[] ds = null;
Destination d = null;
while (itr.hasNext()) {
realuid = (DestinationUID) itr.next();
ds = DL.getDestination(con.getPartitionedStore(), realuid);
d = ds[0];
if (duid.isWildcard() && d.isTemporary()) {
logger.log(Logger.DEBUG, "L10N-XXX: Wildcard production with destination name of " + duid + " to temporary destination " + d.getUniqueName() + " is not supported, ignoring");
continue;
}
if (duid.isWildcard() && d.isInternal()) {
logger.log(Logger.DEBUG, "L10N-XXX: Wildcard production with destination name of " + duid + " to internal destination " + d.getUniqueName() + " is not supported, ignoring");
continue;
}
if (duid.isWildcard() && d.isDMQ()) {
logger.log(Logger.DEBUG, "L10N-XXX: Wildcard production with destination name of " + duid + " to the DeadMessageQueue" + d.getUniqueName() + " is not supported, ignoring");
continue;
}
d.addProducer(p);
}
}
return p;
}
Aggregations