use of com.sun.messaging.jmq.util.JMQXid in project openmq by eclipse-ee4j.
the class GetTransactionsHandler method getTransactionInfo.
private static Hashtable getTransactionInfo(TransactionList tl, TransactionUID id, int type, boolean showpartition) {
Logger logger = Globals.getLogger();
TransactionState ts = tl.retrieveState(id);
if (type == LOCAL) {
ts = tl.retrieveState(id, true);
} else if (type == CLUSTER) {
ts = tl.retrieveState(id, true);
} else if (type == REMOTE) {
ts = tl.getRemoteTransactionState(id);
}
if (ts == null) {
return null;
}
JMQXid xid = tl.UIDToXid(id);
Hashtable table = new Hashtable();
table.put("type", Integer.valueOf(type));
if (xid != null) {
table.put("xid", xid.toString());
}
PartitionedStore pstore = tl.getPartitionedStore();
table.put("txnid", Long.valueOf(id.longValue()) + (showpartition ? "[" + pstore.getPartitionID() + (pstore.isPrimaryPartition() ? "*]" : "]") : ""));
if (ts.getUser() != null) {
table.put("user", ts.getUser());
}
if (ts.getClientID() != null) {
table.put("clientid", ts.getClientID());
}
table.put("timestamp", Long.valueOf(System.currentTimeMillis() - id.age()));
table.put("connection", ts.getConnectionString());
table.put("nmsgs", Integer.valueOf(tl.retrieveNSentMessages(id)));
if (type != REMOTE) {
table.put("nacks", Integer.valueOf(tl.retrieveNConsumedMessages(id)));
} else {
table.put("nacks", Integer.valueOf(tl.retrieveNRemoteConsumedMessages(id)));
}
table.put("state", Integer.valueOf(ts.getState()));
ConnectionUID cuid = ts.getConnectionUID();
if (cuid != null) {
table.put("connectionid", Long.valueOf(cuid.longValue()));
}
TransactionBroker homeBroker = tl.getRemoteTransactionHomeBroker(id);
String homeBrokerStr = "";
if (homeBroker != null) {
homeBrokerStr = homeBroker.getBrokerAddress().toString();
}
table.put("homebroker", homeBrokerStr);
TransactionBroker[] brokers = null;
if (type != REMOTE) {
try {
brokers = tl.getClusterTransactionBrokers(id);
} catch (BrokerException be) {
logger.log(Logger.WARNING, "Exception caught while obtaining list of brokers in transaction", be);
}
}
StringBuilder allBrokers = new StringBuilder();
StringBuilder pendingBrokers = new StringBuilder();
if (brokers != null) {
for (int i = 0; i < brokers.length; ++i) {
TransactionBroker oneBroker = brokers[i];
BrokerAddress addr = oneBroker.getBrokerAddress();
if (allBrokers.length() != 0) {
allBrokers.append(", ");
}
allBrokers.append(addr);
if (oneBroker.isCompleted()) {
continue;
}
if (pendingBrokers.length() != 0) {
pendingBrokers.append(", ");
}
pendingBrokers.append(addr);
}
}
table.put("allbrokers", allBrokers.toString());
table.put("pendingbrokers", pendingBrokers.toString());
return table;
}
use of com.sun.messaging.jmq.util.JMQXid in project openmq by eclipse-ee4j.
the class LocalTxnConverter method convert.
void convert(TransactionInformation txnInfo) throws BrokerException {
if (ToTxnLogConverter.DEBUG) {
logger.log(Logger.DEBUG, getPrefix() + " convertLocalToTxnLogFormat " + txnInfo);
}
// should be a prepared transaction
int state = txnInfo.getState().getState();
if (state != TransactionState.PREPARED) {
String msg = getPrefix() + " convertLocalToTxnLogFormat: ignoring state " + state + " for " + txnInfo;
logger.log(Logger.INFO, msg);
}
TransactionWork txnWork = new TransactionWork();
getSentMessages(txnInfo, txnWork);
getConsumedMessages(txnInfo, txnWork);
TransactionUID txid = txnInfo.getTID();
JMQXid xid = txnInfo.getState().getXid();
LocalTransaction localTxn = new LocalTransaction(txid, state, xid, txnWork);
TransactionState newState = new TransactionState(txnInfo.getState());
localTxn.setTransactionState(newState);
store.logTxn(localTxn);
deleteSentMessagesFromStore(txnWork);
}
use of com.sun.messaging.jmq.util.JMQXid in project openmq by eclipse-ee4j.
the class AckHandler method handleTransaction.
public void handleTransaction(TransactionList translist, IMQConnection con, TransactionUID tid, SysMessageID[] ids, ConsumerUID[] cids, int deliverCnt) throws BrokerException {
for (int i = 0; i < ids.length; i++) {
Consumer consumer = null;
try {
// lookup the session by consumerUID
Session s = Session.getSession(cids[i]);
// look up the consumer by consumerUID
consumer = Consumer.getConsumer(cids[i]);
if (consumer == null) {
throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.I_ACK_FAILED_NO_CONSUMER, cids[i]) + "[TID=" + tid + "]", Status.NOT_FOUND);
}
// try and find the stored consumerUID
ConsumerUID sid = consumer.getStoredConsumerUID();
// if we still dont have a session, attempt to find one
if (s == null) {
SessionUID suid = consumer.getSessionUID();
s = Session.getSession(suid);
}
if (s == null) {
if (BrokerStateHandler.isShutdownStarted()) {
throw new BrokerException(BrokerResources.I_ACK_FAILED_BROKER_SHUTDOWN);
}
throw new BrokerException(br.getKString(br.I_ACK_FAILED_NO_SESSION, "[" + ids[i] + ", " + cids[i] + "]TID=" + tid));
}
if (DEBUG) {
logger.log(logger.INFO, "handleTransaction.addAck[" + i + ", " + ids.length + "]:tid=" + tid + ", sysid=" + ids[i] + ", cid=" + cids[i] + ", sid=" + sid + " on connection " + con);
}
boolean isxa = translist.addAcknowledgement(tid, ids[i], cids[i], sid);
BrokerAddress addr = (BrokerAddress) s.ackInTransaction(cids[i], ids[i], tid, isxa, deliverCnt);
if (addr != null && addr != Globals.getMyAddress()) {
translist.setAckBrokerAddress(tid, ids[i], cids[i], addr);
}
if (fi.FAULT_INJECTION) {
if (fi.checkFault(fi.FAULT_TXN_ACK_1_5, null)) {
fi.unsetFault(fi.FAULT_TXN_ACK_1_5);
TransactionAckExistException tae = new TransactionAckExistException("FAULT:" + fi.FAULT_TXN_ACK_1_5, Status.GONE);
tae.setRemoteConsumerUIDs(String.valueOf(cids[i].longValue()));
tae.setRemote(true);
consumer.recreationRequested();
throw tae;
}
}
} catch (Exception ex) {
String emsg = "[" + ids[i] + ", " + cids[i] + "]TUID=" + tid;
if ((ex instanceof BrokerException) && ((BrokerException) ex).getStatusCode() != Status.ERROR) {
logger.log(Logger.WARNING, Globals.getBrokerResources().getKString(BrokerResources.E_TRAN_ACK_PROCESSING_FAILED, emsg, ex.getMessage()), ex);
} else {
logger.log(Logger.ERROR, Globals.getBrokerResources().getKString(BrokerResources.E_TRAN_ACK_PROCESSING_FAILED, emsg, ex.getMessage()), ex);
}
int state = -1;
JMQXid xid = null;
try {
TransactionState ts = translist.retrieveState(tid);
if (ts != null) {
state = ts.getState();
xid = ts.getXid();
}
translist.updateState(tid, TransactionState.FAILED, true);
} catch (Exception e) {
if (!(e instanceof UnknownTransactionException)) {
String[] args = { TransactionState.toString(state), TransactionState.toString(TransactionState.FAILED), tid.toString(), (xid == null ? "null" : xid.toString()) };
logger.log(Logger.WARNING, Globals.getBrokerResources().getKString(BrokerResources.W_UPDATE_TRAN_STATE_FAIL, args));
}
}
if (ex instanceof TransactionAckExistException) {
PacketReference ref = DL.get(null, ids[i]);
if (ref != null && (ref.isOverrided() || ref.isOverriding())) {
((BrokerException) ex).overrideStatusCode(Status.GONE);
((BrokerException) ex).setRemoteConsumerUIDs(String.valueOf(cids[i].longValue()));
((BrokerException) ex).setRemote(true);
consumer.recreationRequested();
}
}
if (ex instanceof BrokerException) {
throw (BrokerException) ex;
}
throw new BrokerException("Internal Error: Unable to " + " complete processing acknowledgements in a tranaction: " + ex, ex);
}
}
}
use of com.sun.messaging.jmq.util.JMQXid in project openmq by eclipse-ee4j.
the class ProtocolHandler method recover.
/**
* Recover XA Transactions (XA only) - returns Xids
*/
public JMQXid[] recover(int xaflags) throws JMSException {
ReadWritePacket pkt = new ReadWritePacket();
pkt.setPacketType(PacketType.RECOVER_TRANSACTION);
Hashtable ht = new Hashtable(1);
ht.put("JMQXAFlags", Integer.valueOf(xaflags));
pkt.setProperties(ht);
int statusCode = -1;
int quantity = 0;
ReadOnlyPacket replypkt = writePacketWithReply(pkt, PacketType.RECOVER_TRANSACTION_REPLY);
try {
Hashtable replyProps = replypkt.getProperties();
Integer value = (Integer) replyProps.get("JMQStatus");
statusCode = value.intValue();
value = (Integer) replyProps.get("JMQQuantity");
quantity = value.intValue();
} catch (IOException | ClassNotFoundException e) {
ExceptionHandler.handleException(e, ClientResources.X_NET_ACK);
}
if (statusCode != Status.OK) {
// String errorString = AdministeredObject.cr.getKString(AdministeredObject.cr.X_SERVER_ERROR);
// throw new com.sun.messaging.jms.JMSException(errorString, AdministeredObject.cr.X_SERVER_ERROR);
this.throwServerErrorException(replypkt);
}
byte[] body = ((ReadWritePacket) replypkt).getMessageBody();
JMQXid[] xids;
if (body == null || body.length == 0) {
xids = new JMQXid[] {};
} else {
int return_qty = body.length / JMQXid.size();
if ((body.length % JMQXid.size() != 0) || (return_qty != quantity)) {
ExceptionHandler.handleException(new StreamCorruptedException(), ClientResources.X_NET_READ_PACKET);
}
xids = new JMQXid[quantity];
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(body));
for (int i = 0; i < quantity; i++) {
try {
xids[i] = JMQXid.read(dis);
} catch (IOException ioe) {
ExceptionHandler.handleException(ioe, ClientResources.X_NET_READ_PACKET);
}
}
}
return xids;
}
use of com.sun.messaging.jmq.util.JMQXid in project openmq by eclipse-ee4j.
the class VerifyTransactionHandler method handle.
/**
* Method to handle Destination (create or delete) messages
*/
@Override
public boolean handle(IMQConnection con, Packet msg) throws BrokerException {
int status = Status.OK;
String reason = null;
TransactionList[] tls = DL.getTransactionList(con.getPartitionedStore());
TransactionList translist = tls[0];
assert msg.getPacketType() == PacketType.VERIFY_TRANSACTION;
Packet pkt = new Packet(con.useDirectBuffers());
pkt.setConsumerID(msg.getConsumerID());
pkt.setPacketType(PacketType.VERIFY_TRANSACTION_REPLY);
Hashtable hash = new Hashtable();
Hashtable props = null;
TransactionUID tuid = null;
JMQXid xid = null;
try {
props = msg.getProperties();
Long ttid = (Long) props.get("JMQTransactionID");
if (ttid == null) {
throw new BrokerException("Bad/Missing transaction id");
}
ByteBuffer body = msg.getMessageBodyByteBuffer();
if (body != null) {
JMQByteBufferInputStream bbis = new JMQByteBufferInputStream(body);
try {
xid = JMQXid.read(new DataInputStream(bbis));
} catch (IOException e) {
logger.logStack(Logger.ERROR, BrokerResources.E_INTERNAL_BROKER_ERROR, "Could not decode xid from packet " + PacketType.getString(msg.getPacketType()), e);
BrokerException bex = new BrokerException(e.getMessage(), Status.BAD_REQUEST);
bex.initCause(e);
throw bex;
}
}
long tid = ttid.longValue();
if (tid != 0) {
tuid = new TransactionUID(tid);
} else if (xid != null) {
tuid = translist.xidToUID(xid);
if (tuid == null) {
String emsg = Globals.getBrokerResources().getKString(BrokerResources.W_UNKNOWN_XID, "" + xid, PacketType.getString(msg.getPacketType()));
logger.log(Logger.WARNING, emsg);
throw new BrokerException("Unknown XID " + xid, Status.NOT_FOUND);
}
} else {
logger.log(Logger.ERROR, BrokerResources.E_INTERNAL_BROKER_ERROR, "No transaction ID in " + PacketType.getString(msg.getPacketType()));
throw new BrokerException("No transaction ID", Status.BAD_REQUEST);
}
TransactionState ts = translist.retrieveState(tuid, true);
if (ts == null) {
status = Status.GONE;
} else {
int realstate = ts.getState();
if (realstate != TransactionState.PREPARED) {
throw new BrokerException("Transaction " + tuid + " is not PREPARED " + ts, Status.GONE);
}
Hashtable m = translist.getTransactionMap(tuid, true);
// write it to the body
if (m != null) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(m);
oos.flush();
bos.flush();
pkt.setMessageBody(bos.toByteArray());
bos.close();
} catch (Exception ex) {
logger.logStack(Logger.WARNING, BrokerResources.E_INTERNAL_BROKER_ERROR, " sending back broker data", ex);
}
}
}
} catch (BrokerException ex) {
reason = ex.getMessage();
status = ex.getStatusCode();
if (status != Status.GONE && status != Status.NOT_FOUND) {
logger.logStack(Logger.INFO, BrokerResources.E_INTERNAL_BROKER_ERROR, "unknown status", ex);
} else {
logger.log(Logger.DEBUG, "Transaction " + tuid + " not found", ex);
}
} catch (Throwable ex) {
logger.logStack(Logger.INFO, BrokerResources.E_INTERNAL_BROKER_ERROR, "exception processing verify transaction ", ex);
reason = ex.toString();
status = Status.ERROR;
}
if (status == Status.GONE) {
// convert to NF
status = Status.NOT_FOUND;
}
hash.put("JMQStatus", Integer.valueOf(status));
if (reason != null) {
hash.put("JMQReason", reason);
}
if (((IMQBasicConnection) con).getDumpPacket() || ((IMQBasicConnection) con).getDumpOutPacket()) {
hash.put("JMQReqID", msg.getSysMessageID().toString());
}
pkt.setProperties(hash);
con.sendControlMessage(pkt);
return true;
}
Aggregations