use of com.sun.messaging.jmq.io.Packet in project openmq by eclipse-ee4j.
the class GrizzlyMQPacketFilter method handleRead.
/**
* Method is called, when new data was read from the Connection and ready to be processed.
*
* We override this method to perform Buffer -> GIOPMessage transformation.
*
* @param ctx Context of {@link FilterChainContext} processing
* @return the next action
*/
@Override
public NextAction handleRead(final FilterChainContext ctx) throws IOException {
final GrizzlyMQPacketList packetList = GrizzlyMQPacketList.create();
final Connection c = ctx.getConnection();
final Buffer buf = ctx.getMessage();
final PacketParseState parsestate = parsestateAttr.get(c);
while (buf.hasRemaining()) {
int buflen = buf.remaining();
if (DEBUG) {
logger.log(logger.INFO, "[@" + c.hashCode() + "]buflen=" + buflen + ", gotpsize=" + parsestate.gotpsize + ", psize=" + parsestate.psize + ", pos=" + buf.position());
}
if (!parsestate.gotpsize) {
if (buflen < Packet.HEADER_SIZE) {
if (DEBUG) {
logger.log(logger.INFO, "[@" + c.hashCode() + "] not enough for header size " + Packet.HEADER_SIZE);
}
// return ctx.getStopAction(buf);
break;
}
int pos = buf.position();
parsestate.psize = GrizzlyMQPacket.parsePacketSize(buf);
buf.position(pos);
parsestate.gotpsize = true;
}
if (buflen < parsestate.psize) {
if (DEBUG) {
logger.log(logger.INFO, "[@" + c.hashCode() + "] not enough for packet size " + parsestate.psize);
}
// return ctx.getStopAction(buf);
break;
}
if (DEBUG) {
logger.log(logger.INFO, "[@" + c.hashCode() + "]reading packet at pos=" + buf.position() + ", size=" + parsestate.psize);
}
final int pos = buf.position();
Packet pkt = null;
BufferInputStream bis = null;
try {
// XXX
pkt = new GrizzlyMQPacket(false);
pkt.generateSequenceNumber(false);
pkt.generateTimestamp(false);
bis = new BufferInputStream(buf);
pkt.readPacket(bis);
// ctx.setMessage(pkt);
if (DEBUG) {
logger.log(logger.INFO, "[@" + c.hashCode() + "]read packet: " + pkt + ", pre-pos=" + pos);
}
packetList.getPackets().add(pkt);
} catch (OutOfMemoryError err) {
Globals.handleGlobalError(err, Globals.getBrokerResources().getKString(BrokerResources.M_LOW_MEMORY_READALLOC) + ": " + (pkt == null ? "null" : pkt.headerToString()));
buf.position(pos);
try {
Thread.sleep(1000L);
} catch (Exception e) {
}
continue;
} catch (BigPacketException e) {
GrizzlyMQIPConnection conn = connAttr.get(c);
conn.handleBigPacketException(pkt, e);
} catch (IllegalArgumentException e) {
GrizzlyMQIPConnection conn = connAttr.get(c);
conn.handleIllegalArgumentExceptionPacket(pkt, e);
} finally {
if (bis != null) {
bis.close();
}
}
buf.position(pos + parsestate.psize);
parsestate.reset();
}
// There are no packets parsed
if (packetList.getPackets().isEmpty()) {
packetList.recycle(false);
return ctx.getStopAction(buf);
}
final Buffer remainder;
if (buf.hasRemaining()) {
remainder = buf.split(buf.position());
} else {
remainder = null;
}
packetList.setPacketsBuffer(buf);
ctx.setMessage(packetList);
if (DEBUG) {
logger.log(logger.INFO, "[@" + c.hashCode() + "]handleRead.return: " + (remainder == null ? "no remainder" : "remainer=" + remainder.hasRemaining() + ", remaining=" + remainder.remaining()));
}
return ctx.getInvokeAction(remainder);
}
use of com.sun.messaging.jmq.io.Packet in project openmq by eclipse-ee4j.
the class IMQBasicConnection method sendConsumerInfo.
@Override
protected void sendConsumerInfo(int requestType, String destName, int destType, int infoType) {
if (state >= STATE_CLOSED) {
return;
}
Packet info_pkt = new Packet(useDirectBuffers());
info_pkt.setPacketType(PacketType.INFO);
Hashtable props = new Hashtable();
props.put("JMQRequestType", Integer.valueOf(requestType));
props.put("JMQStatus", Status.OK);
info_pkt.setProperties(props);
Hashtable hash = new Hashtable();
hash.put("JMQDestination", destName);
hash.put("JMQDestType", Integer.valueOf(destType));
hash.put("JMQConsumerInfoType", Integer.valueOf(infoType));
DestinationUID duid = null;
try {
duid = DestinationUID.getUID(destName, destType);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(hash);
oos.flush();
bos.flush();
info_pkt.setMessageBody(bos.toByteArray());
bos.close();
} catch (Throwable t) {
logger.log(Logger.WARNING, "XXXI18N Error: Unable to send consumer info to client: " + duid, t);
return;
}
sendControlMessage(info_pkt);
}
use of com.sun.messaging.jmq.io.Packet in project openmq by eclipse-ee4j.
the class JDBCStore method updateTransactionStateWithWorkInternal.
public void updateTransactionStateWithWorkInternal(TransactionUID tid, TransactionState ts, TransactionWork txnwork, long storeSessionID, boolean sync) throws BrokerException {
if (tid == null || ts == null || txnwork == null) {
throw new NullPointerException();
}
if (DEBUG) {
logger.log(Logger.INFO, "JDBCStore.updateTransactionStateInternal(" + tid + ", " + ts + ", " + ", " + txnwork + ", " + sync + ")");
}
Connection conn = null;
Exception myex = null;
try {
conn = dbmgr.getConnection(false);
Util.RetryStrategy retry = null;
do {
boolean inside = false;
try {
Iterator<TransactionWorkMessage> itr1 = txnwork.getSentMessages().iterator();
while (itr1.hasNext()) {
TransactionWorkMessage txnmsg = itr1.next();
Packet m = txnmsg.getMessage();
if (m == null) {
continue;
}
inside = true;
daoFactory.getMessageDAO().insert(conn, txnmsg.getDestUID(), m, null, null, storeSessionID, m.getTimestamp(), true, false);
inside = false;
}
if (FI.FAULT_INJECTION) {
try {
FI.checkFaultAndThrowBrokerException(FaultInjection.FAULT_TXN_PERSIST_WORK_1_5, null);
} catch (BrokerException e) {
FI.unsetFault(FI.FAULT_TXN_PERSIST_WORK_1_5);
throw e;
}
}
List<TransactionWorkMessageAck> txnacks = txnwork.getMessageAcknowledgments();
if (txnacks != null) {
Iterator<TransactionWorkMessageAck> itr2 = txnacks.iterator();
while (itr2.hasNext()) {
TransactionWorkMessageAck txnack = itr2.next();
TransactionAcknowledgement ta = txnack.getTransactionAcknowledgement();
if (ta != null) {
inside = true;
daoFactory.getConsumerStateDAO().updateTransaction(conn, ta.getSysMessageID(), ta.getStoredConsumerUID(), tid);
inside = false;
}
}
}
inside = true;
daoFactory.getTransactionDAO().updateTransactionState(conn, tid, ts, false);
inside = false;
conn.commit();
return;
} catch (Exception e) {
if (!inside) {
try {
conn.rollback();
} catch (SQLException rbe) {
logger.logStack(Logger.WARNING, BrokerResources.X_DB_ROLLBACK_FAILED, rbe);
}
}
if (retry == null) {
retry = new Util.RetryStrategy();
}
try {
retry.assertShouldRetry(e, conn);
} catch (RetrySQLRecoverableException ee) {
try {
Util.close(null, null, conn, ee);
conn = dbmgr.getConnection(false);
} catch (Exception eee) {
logger.logStack(Logger.WARNING, eee.getMessage(), eee);
conn = null;
if (e instanceof BrokerException) {
throw (BrokerException) e;
}
throw new BrokerException(e.getMessage(), e);
}
}
}
} while (true);
} catch (BrokerException e) {
myex = e;
throw e;
} finally {
Util.close(null, null, conn, myex);
}
}
use of com.sun.messaging.jmq.io.Packet in project openmq by eclipse-ee4j.
the class DBTool method doBackup.
/**
* Backup the JDBC store to filebased backup files.
*/
private void doBackup() throws BrokerException {
if (!Globals.getHAEnabled()) {
throw new BrokerException(br.getKString(BrokerResources.I_HA_NOT_ENABLE, dbmgr.getBrokerID()));
}
// instantiate the file store.
Properties props = System.getProperties();
String clusterID = Globals.getClusterID();
String backupDir = (String) props.get(Globals.IMQ + ".backupdir") + File.separator + clusterID;
logger.logToAll(Logger.INFO, "Backup persistent store for HA cluster " + clusterID);
final FileStore fileStore = new FileStore(backupDir, false);
// for backup, need to clear the store before storing anything.
fileStore.clearAll(false);
JDBCStore jdbcStore = null;
try {
jdbcStore = (JDBCStore) StoreManager.getStore();
/*
* For data that are not broker specific, i.e. properties, change records, consumers, brokers, and sessions, we will
* store those data on the top level directory (a.k.a cluster filestore).
*/
// Properties table
Properties properties = jdbcStore.getAllProperties();
Iterator propItr = properties.entrySet().iterator();
while (propItr.hasNext()) {
Map.Entry entry = (Map.Entry) propItr.next();
fileStore.updateProperty((String) entry.getKey(), entry.getValue(), false);
}
propItr = null;
properties = null;
// Configuration Change Record table
List<ChangeRecordInfo> records = jdbcStore.getAllConfigRecords();
for (int i = 0, len = records.size(); i < len; i++) {
fileStore.storeConfigChangeRecord(records.get(i).getTimestamp(), records.get(i).getRecord(), false);
}
records = null;
// Consumer table
Consumer[] consumerArray = jdbcStore.getAllInterests();
for (int i = 0; i < consumerArray.length; i++) {
fileStore.storeInterest(consumerArray[i], false);
}
consumerArray = null;
// Broker & Session table - store all HABrokerInfo as a property
HashMap bkrMap = jdbcStore.getAllBrokerInfos(true);
List haBrokers = new ArrayList(bkrMap.values());
fileStore.updateProperty(STORE_PROPERTY_HABROKERS, haBrokers, true);
/*
* For each broker in the cluster, we will store broker specific data, destinations, messages, transactions,
* acknowledgements in their own filestore (a.k.a broker filestore).
*/
Iterator bkrItr = haBrokers.iterator();
while (bkrItr.hasNext()) {
// Backup data for each broker
HABrokerInfo bkrInfo = (HABrokerInfo) bkrItr.next();
String brokerID = bkrInfo.getId();
logger.logToAll(Logger.INFO, "Backup persistent data for broker " + brokerID);
FileStore bkrFS = null;
JMSBridgeStore jmsbridgeStore = null;
try {
String instanceRootDir = backupDir + File.separator + brokerID;
bkrFS = new FileStore(instanceRootDir, false);
bkrFS.clearAll(false);
// Destination table.
Destination[] dstArray = jdbcStore.getAllDestinations(brokerID);
for (int i = 0, len = dstArray.length; i < len; i++) {
DestinationUID did = dstArray[i].getDestinationUID();
Destination dst = bkrFS.getDestination(did);
if (dst == null) {
// Store the destination if not found
bkrFS.storeDestination(dstArray[i], false);
}
}
// Storing messages for each destination.
for (int i = 0, len = dstArray.length; i < len; i++) {
Enumeration e = jdbcStore.messageEnumeration(dstArray[i]);
try {
for (; e.hasMoreElements(); ) {
DestinationUID did = dstArray[i].getDestinationUID();
Packet message = (Packet) e.nextElement();
SysMessageID mid = message.getSysMessageID();
// Get interest states for the message; Consumer State table
HashMap stateMap = jdbcStore.getInterestStates(did, mid);
if (stateMap == null || stateMap.isEmpty()) {
bkrFS.storeMessage(did, message, false);
} else {
int size = stateMap.size();
ConsumerUID[] iids = new ConsumerUID[size];
int[] states = new int[size];
Iterator stateItr = stateMap.entrySet().iterator();
int j = 0;
while (stateItr.hasNext()) {
Map.Entry entry = (Map.Entry) stateItr.next();
iids[j] = (ConsumerUID) entry.getKey();
states[j] = ((Integer) entry.getValue()).intValue();
j++;
}
bkrFS.storeMessage(did, message, iids, states, false);
}
}
} finally {
jdbcStore.closeEnumeration(e);
}
}
// Transaction table
Collection txnList = jdbcStore.getTransactions(brokerID);
Iterator txnItr = txnList.iterator();
while (txnItr.hasNext()) {
TransactionUID tid = (TransactionUID) txnItr.next();
TransactionInfo txnInfo = jdbcStore.getTransactionInfo(tid);
TransactionAcknowledgement[] txnAck = jdbcStore.getTransactionAcks(tid);
bkrFS.storeTransaction(tid, txnInfo, false);
for (int i = 0, len = txnAck.length; i < len; i++) {
bkrFS.storeTransactionAck(tid, txnAck[i], false);
}
}
/**
************************************************
* JMSBridge
*************************************************
*/
Properties bp = new Properties();
bp.setProperty("instanceRootDir", instanceRootDir);
bp.setProperty("reset", "true");
bp.setProperty("logdomain", "imqdbmgr");
bp.setProperty("identityName", Globals.getIdentityName());
BridgeServiceManager.getExportedService(JMSBridgeStore.class, "JMS", bp);
List bnames = jdbcStore.getJMSBridgesByBroker(brokerID, null);
Collections.sort(bnames);
String bname = null;
String currbname = null;
Iterator itr = bnames.iterator();
while (itr.hasNext()) {
bname = (String) itr.next();
if (currbname == null || !currbname.equals(bname)) {
currbname = bname;
bp.setProperty("jmsbridge", bname);
if (jmsbridgeStore != null) {
jmsbridgeStore.closeJMSBridgeStore();
jmsbridgeStore = null;
}
logger.logToAll(logger.INFO, "Backup JMS bridge " + bname);
jmsbridgeStore = (JMSBridgeStore) BridgeServiceManager.getExportedService(JMSBridgeStore.class, "JMS", bp);
List lrs = jdbcStore.getLogRecordsByNameByBroker(bname, brokerID, null);
logger.logToAll(logger.INFO, "\tBackup JMS bridge " + bname + " with " + lrs.size() + " TM log records");
byte[] lr = null;
Iterator itr1 = lrs.iterator();
while (itr1.hasNext()) {
lr = (byte[]) itr1.next();
jmsbridgeStore.storeTMLogRecord(null, lr, currbname, true, null);
}
}
}
} finally {
bkrFS.close();
if (jmsbridgeStore != null) {
jmsbridgeStore.closeJMSBridgeStore();
}
}
}
logger.logToAll(Logger.INFO, "Backup persistent store complete.");
} catch (Throwable ex) {
ex.printStackTrace();
throw new BrokerException(ex.getMessage());
} finally {
assert fileStore != null;
try {
fileStore.close();
} catch (Exception e) {
e.printStackTrace();
}
StoreManager.releaseStore(true);
}
}
use of com.sun.messaging.jmq.io.Packet in project openmq by eclipse-ee4j.
the class UpgradeHAStore method doUpgradeMsg.
private void doUpgradeMsg(Connection conn) throws BrokerException {
MessageDAO msgDAO = dbMgr.getDAOFactory().getMessageDAO();
String oldmsgtbl = MessageDAO.TABLE_NAME_PREFIX + "S" + brokerID;
HashMap msgToDst = new HashMap();
// SQL to select all messages from Non-HA table
String getAllMsgFromOldSQL = new StringBuilder(128).append("SELECT ").append(MessageDAO.ID_COLUMN).append(", ").append(MessageDAO.MESSAGE_COLUMN).append(", ").append(MessageDAO.DESTINATION_ID_COLUMN).append(", ").append(MessageDAO.STORE_SESSION_ID_COLUMN).append(", ").append(MessageDAO.CREATED_TS_COLUMN).append(" FROM ").append(oldmsgtbl).toString();
Statement stmt = null;
ResultSet rs = null;
String msgID = null;
Packet msg = null;
Exception myex = null;
try {
stmt = conn.createStatement();
rs = dbMgr.executeQueryStatement(stmt, getAllMsgFromOldSQL);
while (rs.next()) {
msgID = rs.getString(1);
msg = new Packet(false);
msg.generateTimestamp(false);
msg.generateSequenceNumber(false);
InputStream is = null;
Blob blob = rs.getBlob(2);
is = blob.getBinaryStream();
msg.readPacket(is);
is.close();
String dstID = rs.getString(3);
long sessionID = rs.getLong(4);
long createdTS = rs.getLong(5);
try {
msgDAO.insert(conn, dstID, msg, null, null, sessionID, createdTS, true, false);
} catch (BrokerException be) {
// If msg exist, just logged and continue
if (be.getStatusCode() == Status.CONFLICT) {
logger.log(Logger.WARNING, be.getMessage() + ": Ignore");
} else {
throw be;
}
}
msgToDst.put(msgID, dstID);
}
} catch (Exception e) {
myex = e;
String errorMsg = br.getKString(BrokerResources.X_JDBC_UPGRADE_MESSAGES_FAILED, (msg == null ? msgID : msg.getSysMessageID().toString()));
logger.logStack(Logger.ERROR, errorMsg, e);
throw new BrokerException(errorMsg, e);
} finally {
Util.close(rs, stmt, null, myex);
}
// upgrade interest list
ConsumerStateDAO stateDAO = dbMgr.getDAOFactory().getConsumerStateDAO();
String oldstatetbl = ConsumerStateDAO.TABLE_NAME_PREFIX + "S" + brokerID;
// SQL to select all interest states from Non-HA table
String getAllStateFromOldSQL = new StringBuilder(128).append("SELECT ").append(ConsumerStateDAO.MESSAGE_ID_COLUMN).append(", ").append(ConsumerStateDAO.CONSUMER_ID_COLUMN).append(", ").append(ConsumerStateDAO.STATE_COLUMN).append(", ").append(ConsumerStateDAO.TRANSACTION_ID_COLUMN).append(", ").append(ConsumerStateDAO.CREATED_TS_COLUMN).append(" FROM ").append(oldstatetbl).append(" WHERE ").append(TINTSTATE_CSTATE).append(" <> ").append(PartitionedStore.INTEREST_STATE_ACKNOWLEDGED).toString();
String insertStateSQL = new StringBuilder(128).append("INSERT INTO ").append(stateDAO.getTableName()).append(" ( ").append(ConsumerStateDAO.MESSAGE_ID_COLUMN).append(", ").append(ConsumerStateDAO.CONSUMER_ID_COLUMN).append(", ").append(ConsumerStateDAO.STATE_COLUMN).append(", ").append(ConsumerStateDAO.TRANSACTION_ID_COLUMN).append(", ").append(ConsumerStateDAO.CREATED_TS_COLUMN).append(") VALUES ( ?, ?, ?, ?, ? )").toString();
boolean dobatch = dbMgr.supportsBatchUpdates();
PreparedStatement pstmt = null;
String mid = null;
long cuid = 0;
try {
pstmt = dbMgr.createPreparedStatement(conn, insertStateSQL);
stmt = conn.createStatement();
rs = dbMgr.executeQueryStatement(stmt, getAllStateFromOldSQL);
while (rs.next()) {
mid = rs.getString(1);
cuid = rs.getLong(2);
int state = rs.getInt(3);
long txnID = rs.getLong(4);
long createdTS = rs.getLong(5);
String dst = (String) msgToDst.get(mid);
// ignore a state whose dst or message does not exists
if (dst == null) {
continue;
}
try {
pstmt.setString(1, mid);
pstmt.setLong(2, cuid);
pstmt.setInt(3, state);
if (txnID > 0) {
pstmt.setLong(4, txnID);
} else {
pstmt.setNull(4, Types.BIGINT);
}
pstmt.setLong(5, createdTS);
if (dobatch) {
pstmt.addBatch();
} else {
pstmt.executeUpdate();
}
} catch (SQLException e) {
SQLException ex = DBManager.wrapSQLException("[" + insertStateSQL + "]", e);
throw ex;
}
}
msgToDst.clear();
if (dobatch) {
pstmt.executeBatch();
}
conn.commit();
} catch (SQLException e) {
myex = e;
String errorMsg = br.getKString(BrokerResources.X_JDBC_UPGRADE_MESSAGES_FAILED, (mid == null ? "loading" : mid));
logger.logStack(Logger.ERROR, errorMsg, e);
throw new BrokerException(errorMsg, e);
} finally {
Util.close(rs, stmt, null, myex);
Util.close(null, pstmt, null, myex);
}
}
Aggregations