use of com.sun.messaging.jmq.jmsserver.util.BrokerException in project openmq by eclipse-ee4j.
the class ConsumerHandler method _createConsumer.
private Consumer[] _createConsumer(DestinationUID dest_uid, IMQConnection con, Session session, String selectorstr, String clientid, String subscriptionName, boolean durable, boolean shared, boolean jmsshare, boolean nolocal, int size, String consumerString, boolean isIndemp, boolean useFlowControl) throws BrokerException, SelectorFormatException, IOException {
Consumer c = null;
Consumer newc = null;
Subscription sub = null;
String selector = selectorstr;
if (selectorstr != null && selectorstr.trim().length() == 0) {
selector = null;
}
try {
int prefetch = -1;
if (isIndemp) {
// see if we already created it
c = Consumer.getConsumer(consumerString);
if (c != null) {
prefetch = c.getPrefetch();
}
}
if (c == null) {
c = new Consumer(dest_uid, selector, nolocal, con.getConnectionUID());
c.setCreator(consumerString);
newc = c;
newc.pause("Consumer: new consumer");
// OK, determine if we are a wildcard or not
Destination[] ds = DL.getDestination(con.getPartitionedStore(), dest_uid);
// PART
Destination d = ds[0];
boolean wildcard = dest_uid.isWildcard();
// NOTE: if d == null, wildcard == true
int cprefetch = size;
int dprefetch = (wildcard ? -1 : (!shared ? d.getMaxPrefetch() : d.getSharedConsumerFlowLimit()));
prefetch = (dprefetch == -1) ? cprefetch : (cprefetch == -1 ? cprefetch : (cprefetch > dprefetch ? dprefetch : cprefetch));
c.setPrefetch(prefetch, useFlowControl);
// actual subscription added to the destination
if (subscriptionName != null && durable) {
if (!shared) {
logger.log(Logger.INFO, br.getKString(br.I_CREATE_UNSHARED_DURA, Subscription.getDSubLogString(clientid, subscriptionName), dest_uid));
} else {
logger.log(Logger.INFO, br.getKString(br.I_CREATE_SHARED_DURA, Subscription.getDSubLogString(clientid, subscriptionName) + (jmsshare ? "jms" : "mq"), dest_uid));
}
// durable
// get the subscription ... this may throw
// an exception IF we cant
sub = Subscription.findCreateDurableSubscription(clientid, subscriptionName, shared, jmsshare, dest_uid, selector, nolocal, true);
sub.setCreator(consumerString);
sub.pause("Consumer attaching to durable");
// add the consumer .. this may throw an
// exception IF
sub.attachConsumer(c, con);
c.localConsumerCreationReady();
Map<PartitionedStore, LinkedHashSet<Destination>> dmap = DL.findMatchingDestinationMap(null, dest_uid);
LinkedHashSet dset = null;
Iterator<LinkedHashSet<Destination>> itr = dmap.values().iterator();
boolean notify = true;
while (itr.hasNext()) {
dset = itr.next();
if (dset == null) {
continue;
}
Iterator<Destination> itr1 = dset.iterator();
while (itr1.hasNext()) {
Destination dd = itr1.next();
if (dd == null) {
continue;
}
Subscription oldsub = null;
try {
oldsub = (Subscription) dd.addConsumer(sub, notify, con);
} catch (ConsumerAlreadyAddedException e) {
logger.log(logger.INFO, e.getMessage());
}
if (oldsub != null) {
oldsub.purge();
}
}
notify = false;
}
sub.sendCreateSubscriptionNotification(c);
} else if ((wildcard || !d.isQueue()) && shared) {
logger.log(Logger.INFO, br.getKString(br.I_CREATE_SHARED_NONDURA_SUB, Subscription.getNDSubLongLogString(clientid, dest_uid, selectorstr, subscriptionName, nolocal), "" + dest_uid));
sub = Subscription.createAttachNonDurableSub(c, con, subscriptionName, shared, jmsshare);
c.localConsumerCreationReady();
if (sub != null) {
sub.pause("Consumer: attaching to nondurable");
Map<PartitionedStore, LinkedHashSet<Destination>> dmap = DL.findMatchingDestinationMap(null, dest_uid);
LinkedHashSet dset = null;
Iterator<LinkedHashSet<Destination>> itr = dmap.values().iterator();
while (itr.hasNext()) {
dset = itr.next();
if (dset == null) {
continue;
}
Iterator<Destination> itr1 = dset.iterator();
while (itr1.hasNext()) {
Destination dd = itr1.next();
if (dd != null) {
dd.addConsumer(sub, true, con);
}
}
}
}
c.attachToConnection(con.getConnectionUID());
if (sub != null) {
sub.sendCreateSubscriptionNotification(c);
}
} else {
c.localConsumerCreationReady();
// non-durable
Destination dd = null;
Map<PartitionedStore, LinkedHashSet<Destination>> dmap = DL.findMatchingDestinationMap(null, dest_uid);
LinkedHashSet dset = null;
Iterator<LinkedHashSet<Destination>> itr = dmap.values().iterator();
while (itr.hasNext()) {
dset = itr.next();
if (dset == null) {
continue;
}
Iterator<Destination> itr1 = dset.iterator();
while (itr1.hasNext()) {
dd = itr1.next();
if (dd != null) {
dd.addConsumer(c, true, con);
}
}
}
c.attachToConnection(con.getConnectionUID());
c.sendCreateConsumerNotification();
}
}
if (fi.FAULT_INJECTION) {
// e.g. imqcmd debug fault -n consumer.add.1 -o selector="mqDestinationName = 'T:t0.*'" -debug
HashMap fips = new HashMap();
fips.put(FaultInjection.DST_NAME_PROP, DestinationUID.getUniqueString(dest_uid.getName(), dest_uid.isQueue()));
fi.checkFaultAndSleep(FaultInjection.FAULT_CONSUMER_ADD_1, fips);
}
session.attachConsumer(c);
if (DEBUG) {
logger.log(logger.INFO, "Attached consumer " + c + "[prefetch=" + c.getPrefetch() + ", useConsumerFlowControl=" + useFlowControl + "] to session " + session);
}
Consumer[] retc = new Consumer[3];
retc[0] = c;
retc[1] = newc;
retc[2] = sub;
return retc;
} catch (Exception e) {
Object[] args = { (durable ? Subscription.getDSubLogString(clientid, subscriptionName) : (shared ? Subscription.getNDSubLongLogString(clientid, dest_uid, selector, subscriptionName, nolocal) : "")), con, dest_uid };
String emsg = Globals.getBrokerResources().getKString(BrokerResources.W_ADD_AUTO_CONSUMER_FAILED, args);
logger.logStack(logger.ERROR, emsg, e);
try {
if (c != null) {
try {
session.detatchConsumer(c.getConsumerUID(), null, false, false, false);
} catch (Exception e1) {
try {
c.destroyConsumer((new HashSet()), null, true, false, true);
} catch (Exception e2) {
}
}
}
Map<PartitionedStore, LinkedHashSet<Destination>> dmap = DL.findMatchingDestinationMap(null, dest_uid);
LinkedHashSet dset = null;
Destination dd = null;
Iterator<LinkedHashSet<Destination>> itr = dmap.values().iterator();
while (itr.hasNext()) {
dset = itr.next();
if (dset == null) {
continue;
}
Iterator<Destination> itr1 = dset.iterator();
while (itr1.hasNext()) {
dd = itr1.next();
if (dd == null) {
continue;
}
try {
if (c != null) {
dd.removeConsumer(c.getConsumerUID(), true);
}
if (sub != null) {
dd.removeConsumer(sub.getConsumerUID(), true);
}
} catch (Exception e1) {
}
}
}
try {
if (sub != null && c != null) {
sub.releaseConsumer(c.getConsumerUID());
}
} catch (Exception e1) {
}
if (subscriptionName != null && durable) {
if (sub != null && sub.getCreator() != null && sub.getCreator().equals(consumerString)) {
try {
Subscription.unsubscribe(subscriptionName, clientid);
} catch (Exception e1) {
}
}
}
} catch (Exception e3) {
}
if (e instanceof BrokerException) {
throw (BrokerException) e;
}
if (e instanceof IOException) {
throw (IOException) e;
}
if (e instanceof SelectorFormatException) {
throw (SelectorFormatException) e;
}
throw new BrokerException(emsg, e);
}
}
use of com.sun.messaging.jmq.jmsserver.util.BrokerException in project openmq by eclipse-ee4j.
the class ConsumerHandler method destroyConsumer.
public void destroyConsumer(IMQConnection con, Session session, ConsumerUID uid, String durableName, String clientID, SysMessageID lastid, boolean lastidInTransaction, boolean redeliverAll, boolean isIndemp) throws BrokerException {
if (durableName != null) {
Subscription usub = Subscription.unsubscribe(durableName, clientID);
if (usub == null) {
// already destroyed
throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_UNKNOWN_DURABLE_INTEREST, Subscription.getDSubLogString(clientID, durableName)), Status.NOT_FOUND);
}
DestinationUID dest_uid = usub.getDestinationUID();
Destination[] ds = DL.getDestination(con.getPartitionedStore(), dest_uid);
Destination d = ds[0];
if (d != null) {
d.removeConsumer(uid, true);
}
} else {
boolean redeliver = false;
if (con.getClientProtocolVersion() < Connection.RAPTOR_PROTOCOL) {
redeliver = true;
}
if (session == null && !isIndemp) {
if (con.getConnectionState() >= Connection.STATE_CLOSED) {
throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_CONNECTION_CLOSING, con.getConnectionUID()), Status.NOT_FOUND);
} else {
throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_CONSUMER_SESSION_NOT_FOUND, uid, con.getConnectionUID()), Status.NOT_FOUND);
}
}
if (session != null) {
// should only be null w/ indemp
Consumer c = (Consumer) session.detatchConsumer(uid, lastid, lastidInTransaction, redeliver, redeliverAll);
if (DEBUG) {
logger.log(Logger.INFO, "ConsumerHandler: closed consumer " + c + ", with {lastid=" + lastid + ", lastidInTransaction=" + lastidInTransaction + ", redeliver=" + redeliver + ", redeliverAll=" + redeliverAll + ", isindemp=" + isIndemp + "}");
}
DestinationUID dest_uid = c.getDestinationUID();
Destination[] ds = DL.getDestination(con.getPartitionedStore(), dest_uid);
Destination d = ds[0];
if (d != null) {
d.removeConsumer(uid, true);
}
}
}
}
use of com.sun.messaging.jmq.jmsserver.util.BrokerException in project openmq by eclipse-ee4j.
the class GetDurablesHandler method handle.
/**
* Handle the incomming administration message.
*
* @param con The Connection the message came in on.
* @param cmd_msg The administration message
* @param cmd_props The properties from the administration message
*/
@Override
public boolean handle(IMQConnection con, Packet cmd_msg, Hashtable cmd_props) {
if (DEBUG) {
logger.log(Logger.DEBUG, this.getClass().getName() + ": " + cmd_props);
}
String destination = (String) cmd_props.get(MessageType.JMQ_DESTINATION);
// Send reply
Packet reply = new Packet(con.useDirectBuffers());
reply.setPacketType(PacketType.OBJECT_MESSAGE);
int status = Status.OK;
Vector v = null;
String err = null;
try {
DestinationUID duid = null;
if (destination != null) {
duid = DestinationUID.getUID(destination, false);
}
Set s = Subscription.getAllSubscriptions(duid);
v = new Vector();
Iterator itr = s.iterator();
while (itr.hasNext()) {
Subscription sub = (Subscription) itr.next();
DurableInfo di = new DurableInfo();
di.isDurable = sub.isDurable();
di.isShared = sub.getShared();
di.isJMSShared = sub.getJMSShared();
if (di.isDurable) {
di.name = sub.getDurableName();
} else if (di.isJMSShared) {
di.name = sub.getNDSubscriptionName();
}
di.clientID = sub.getClientID();
di.isActive = sub.isActive();
di.uidString = String.valueOf(sub.getConsumerUID().longValue());
List children = sub.getChildConsumers();
di.activeCount = children.size();
di.activeConsumers = new LinkedHashMap<>();
Iterator itr1 = children.iterator();
while (itr1.hasNext()) {
Consumer c = (Consumer) itr1.next();
ConsumerInfo cinfo = new ConsumerInfo();
cinfo.connection = new ConnectionInfo();
cinfo.connection.uuid = c.getConsumerUID().getConnectionUID().longValue();
cinfo.uidString = String.valueOf(c.getConsumerUID().longValue());
ConsumerUID uid = c.getStoredConsumerUID();
if (uid != null) {
cinfo.subuidString = String.valueOf(uid.longValue());
}
BrokerAddress addr = c.getConsumerUID().getBrokerAddress();
if (addr != null) {
cinfo.brokerAddressShortString = addr.getMQAddress().getHostAddressNPort() + (addr.getBrokerID() == null ? "" : "[" + addr.getBrokerID() + "]");
}
di.activeConsumers.put(cinfo.uidString, cinfo);
}
di.nMessages = sub.numInProcessMsgs();
di.consumer = new ConsumerInfo();
// Ok, I'm not setting id because it really should be an int, maybe later
di.consumer.destination = sub.getDestinationUID().getName();
di.consumer.type = DestType.DEST_TYPE_TOPIC;
di.consumer.selector = sub.getSelectorStr();
// not bothering with the connection this time either
di.consumer.connection = null;
v.add(di);
}
} catch (BrokerException ex) {
err = ex.getMessage();
status = ex.getStatusCode();
}
setProperties(reply, MessageType.GET_DURABLES_REPLY, status, err);
setBodyObject(reply, v);
parent.sendReply(con, cmd_msg, reply);
return true;
}
use of com.sun.messaging.jmq.jmsserver.util.BrokerException in project openmq by eclipse-ee4j.
the class MigrateStoreHandler method handle.
/**
* Handle the incomming administration message.
*
* @param con The Connection the message came in on.
* @param cmd_msg The administration message
* @param cmd_props The properties from the administration message
*/
@Override
public boolean handle(IMQConnection con, Packet cmd_msg, Hashtable cmd_props) {
boolean noop = true;
int status = Status.OK;
String errMsg = "";
if (DEBUG) {
logger.log(Logger.INFO, this.getClass().getName() + ": " + "Request migrate this broker''s store: " + cmd_props);
}
String brokerID = (String) cmd_props.get(MessageType.JMQ_BROKER_ID);
String partition = (String) cmd_props.get(MessageType.JMQ_MIGRATESTORE_PARTITION);
if (partition == null) {
if (brokerID != null) {
logger.log(Logger.INFO, BrokerResources.I_ADMIN_MIGRATESTORE_TO, brokerID);
} else {
logger.log(Logger.INFO, BrokerResources.I_ADMIN_MIGRATESTORE);
}
} else {
logger.log(Logger.INFO, "XXXAdmin request migrate this broker's store partition " + partition + " to broker " + brokerID);
}
HAMonitorService hamonitor = Globals.getHAMonitorService();
if (hamonitor != null && hamonitor.inTakeover()) {
status = Status.NOT_MODIFIED;
errMsg = rb.getString(rb.E_CANNOT_PROCEED_TAKEOVER_IN_PROCESS);
logger.log(Logger.ERROR, this.getClass().getName() + ": " + errMsg);
}
if (status == Status.OK) {
if (partition == null && Globals.getHAEnabled()) {
status = Status.NOT_MODIFIED;
errMsg = rb.getKString(rb.E_OPERATION_NOT_SUPPORTED_IN_HA, MessageType.getString(MessageType.MIGRATESTORE_BROKER));
logger.log(Logger.ERROR, errMsg);
}
}
if (status == Status.OK) {
if (Globals.isJMSRAManagedBroker()) {
status = Status.NOT_MODIFIED;
errMsg = "Can not process migration store request because this broker's life cycle is JMSRA managed";
logger.log(Logger.ERROR, this.getClass().getName() + ": " + errMsg);
}
}
UID partitionID = null;
if (status == Status.OK) {
if (partition != null) {
try {
long v = Long.parseLong(partition);
partitionID = new UID(v);
} catch (Exception e) {
partitionID = null;
status = Status.NOT_MODIFIED;
errMsg = "XXXCan not process migration partition " + partition + " request because unable to parse " + partition + ": " + e;
logger.log(Logger.ERROR, errMsg);
}
}
}
if (status == Status.OK) {
if (partitionID != null && brokerID == null) {
status = Status.NOT_MODIFIED;
errMsg = "XXXCan not process migration partition " + partitionID + " request because brokerID not specified";
logger.log(Logger.ERROR, errMsg);
}
}
if (status == Status.OK) {
if (partitionID != null && !(DL.isPartitionMode() && DL.isPartitionMigratable())) {
status = Status.NOT_MODIFIED;
errMsg = "XXXCan not process migration partition " + partitionID + " request because partition mode not enabled";
logger.log(Logger.ERROR, errMsg);
}
}
if (status == Status.OK) {
if (partitionID != null) {
DestinationList dl = DL.getDestinationList(partitionID);
if (dl == null) {
status = Status.NOT_MODIFIED;
errMsg = "XXXCan not process migration partition " + partitionID + " request because partition " + partitionID + " not found";
logger.log(Logger.ERROR, errMsg);
} else if (dl.getPartitionedStore().isPrimaryPartition()) {
status = Status.NOT_MODIFIED;
errMsg = "XXXCan not process migration partition " + partitionID + " request because partition " + partitionID + " is the primary partition";
logger.log(Logger.ERROR, errMsg);
}
}
}
if (status == Status.OK) {
if (brokerID == null) {
try {
brokerID = getBrokerID();
} catch (Throwable t) {
status = Status.NOT_MODIFIED;
errMsg = "Unable to get a connected broker to takeover this broker's store: " + t.getMessage();
if ((t instanceof OperationNotAllowedException) && ((OperationNotAllowedException) t).getOperation().equals(MessageType.getString(MessageType.MIGRATESTORE_BROKER))) {
logger.log(logger.ERROR, errMsg);
} else {
logger.logStack(logger.ERROR, errMsg, t);
}
}
}
}
if (status != Status.OK) {
sendReply(con, cmd_msg, brokerID, null, errMsg, status, null);
return true;
}
try {
BrokerStateHandler.setExclusiveRequestLock(ExclusiveRequest.MIGRATE_STORE);
} catch (Throwable t) {
status = Status.PRECONDITION_FAILED;
if (t instanceof BrokerException) {
status = ((BrokerException) t).getStatusCode();
}
errMsg = MessageType.getString(MessageType.MIGRATESTORE_BROKER) + ": " + Status.getString(status) + " - " + t.getMessage();
logger.log(Logger.ERROR, errMsg);
status = Status.NOT_MODIFIED;
}
try {
if (partitionID != null) {
migratePartition(con, cmd_msg, partitionID, brokerID);
return true;
}
Long syncTimeout = null;
final BrokerStateHandler bsh = Globals.getBrokerStateHandler();
if (status == Status.OK) {
try {
syncTimeout = (Long) cmd_props.get(MessageType.JMQ_MIGRATESTORE_SYNC_TIMEOUT);
ClusterManager cm = Globals.getClusterManager();
BrokerMQAddress self = (BrokerMQAddress) cm.getMQAddress();
BrokerMQAddress master = (cm.getMasterBroker() == null ? null : (BrokerMQAddress) cm.getMasterBroker().getBrokerURL());
if (self.equals(master)) {
throw new BrokerException(rb.getKString(rb.E_CHANGE_MASTER_BROKER_FIRST, MessageType.getString(MessageType.MIGRATESTORE_BROKER)), Status.NOT_ALLOWED);
}
} catch (Throwable t) {
status = Status.PRECONDITION_FAILED;
if (t instanceof BrokerException) {
status = ((BrokerException) t).getStatusCode();
}
errMsg = MessageType.getString(MessageType.MIGRATESTORE_BROKER) + ": " + Status.getString(status) + " - " + t.getMessage();
logger.log(Logger.ERROR, errMsg);
status = Status.NOT_MODIFIED;
}
}
SysMessageID replyMessageID = null;
String replyStatusStr = null;
try {
// shutdown if !noop
String hostport = null;
if (status == Status.OK) {
try {
noop = false;
hostport = bsh.takeoverME(brokerID, syncTimeout, con);
} catch (BrokerException ex) {
status = ex.getStatusCode();
if (status == Status.BAD_REQUEST || status == Status.NOT_ALLOWED || status == Status.NOT_MODIFIED || status == Status.UNAVAILABLE || status == Status.PRECONDITION_FAILED) {
status = Status.PRECONDITION_FAILED;
if (ex instanceof OperationNotAllowedException) {
if (((OperationNotAllowedException) ex).getOperation().equals(MessageType.getString(MessageType.MIGRATESTORE_BROKER))) {
status = Status.NOT_MODIFIED;
noop = true;
}
}
errMsg = Globals.getBrokerResources().getKString(BrokerResources.E_FAIL_MIGRATESTORE_NOT_MIGRATED, ex.getMessage());
if (noop) {
logger.log(Logger.ERROR, errMsg);
} else {
logger.logStack(Logger.ERROR, errMsg, ex);
}
} else {
status = Status.EXPECTATION_FAILED;
errMsg = Globals.getBrokerResources().getKString(BrokerResources.E_FAIL_TAKEOVERME, brokerID, ex.getMessage());
logger.logStack(Logger.ERROR, errMsg, ex);
}
}
}
if (status == Status.OK) {
try {
Globals.getClusterBroadcast().stopClusterIO(false, true, null);
} catch (Throwable t) {
logger.logStack(Logger.WARNING, "Failed to stop cluster IO", t);
}
}
List ret = sendReply(con, cmd_msg, brokerID, hostport, errMsg, status, null);
replyMessageID = (SysMessageID) ret.get(0);
replyStatusStr = (String) ret.get(1);
} finally {
final SysMessageID mid = replyMessageID;
final String statusStr = replyStatusStr;
if (!noop) {
try {
if (con instanceof IMQBasicConnection) {
IMQBasicConnection ipCon = (IMQBasicConnection) con;
ipCon.flushControl(1000);
}
try {
Globals.getServiceManager().stopNewConnections(ServiceType.NORMAL);
} catch (Exception e) {
logger.logStack(logger.WARNING, rb.getKString(rb.W_STOP_SERVICE_FAIL, ServiceType.getServiceTypeString(ServiceType.NORMAL), e.getMessage()), e);
}
try {
Globals.getServiceManager().stopNewConnections(ServiceType.ADMIN);
} catch (Exception e) {
logger.logStack(logger.WARNING, rb.getKString(rb.W_STOP_SERVICE_FAIL, ServiceType.getServiceTypeString(ServiceType.ADMIN), e.getMessage()), e);
}
BrokerStateHandler.setShuttingDown(true);
bsh.prepareShutdown(false, true);
waitForHandlersToComplete(20);
if (mid == null) {
logger.log(Logger.INFO, BrokerResources.I_ADMIN_SHUTDOWN_REQUEST);
bsh.initiateShutdown("admin", 0, false, 0, true);
return true;
}
final String waitstr = rb.getKString(rb.I_WAIT_ADMIN_RECEIVE_REPLY, MessageType.getString(MessageType.MIGRATESTORE_BROKER_REPLY) + "[" + statusStr + "]");
final long totalwait = Globals.getConfig().getIntProperty(MAX_WAIT_ADMIN_CLIENT_PROP, DEFAULT_MAX_WAIT_ADMIN_CLIENT) * 1000L;
final IMQConnection conn = con;
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
try {
long waited = 0L;
while (conn.getConnectionState() < Connection.STATE_CLEANED && waited < totalwait) {
logger.log(logger.INFO, waitstr);
try {
Thread.sleep(500);
waited += 500L;
} catch (Exception e) {
/* ignore */
}
}
logger.log(Logger.INFO, BrokerResources.I_ADMIN_SHUTDOWN_REQUEST);
bsh.initiateShutdown("admin-migratestore-shutdown", 0, false, 0, true);
} catch (Throwable t) {
bsh.initiateShutdown("admin-migratestore-shutdown::[" + t.toString() + "]", 0, false, 0, true);
}
}
});
} catch (Throwable t) {
bsh.initiateShutdown("admin-migratestore-shutdown:[" + t.toString() + "]", 0, false, 0, true);
}
}
}
} finally {
BrokerStateHandler.unsetExclusiveRequestLock(ExclusiveRequest.MIGRATE_STORE);
}
return true;
}
use of com.sun.messaging.jmq.jmsserver.util.BrokerException in project openmq by eclipse-ee4j.
the class ReplaceMessageHandler method getBytesFromMessage.
public static byte[] getBytesFromMessage(HashMap h) throws BrokerException {
byte[] ba = null;
Object msgBody = h.get("MessageBody");
Integer msgType = (Integer) h.get("MessageBodyType");
switch(msgType.intValue()) {
case PacketType.TEXT_MESSAGE:
try {
String textMsg = (String) msgBody;
ba = textMsg.getBytes("UTF8");
} catch (Exception e) {
String emsg = "Caught exception while creating text message body";
throw new BrokerException(emsg, e);
}
break;
case PacketType.BYTES_MESSAGE:
case PacketType.STREAM_MESSAGE:
ba = (byte[]) msgBody;
break;
case PacketType.MAP_MESSAGE:
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
HashMap map = (HashMap) msgBody;
objectOutputStream.writeObject(map);
objectOutputStream.flush();
ba = byteArrayOutputStream.toByteArray();
} catch (Exception e) {
String emsg = "Caught exception while creating map message body";
throw new BrokerException(emsg, e);
}
break;
case PacketType.OBJECT_MESSAGE:
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(msgBody);
objectOutputStream.flush();
ba = byteArrayOutputStream.toByteArray();
} catch (Exception e) {
String emsg = "Caught exception while creating object message body";
throw new BrokerException(emsg, e);
}
break;
default:
String emsg = "Unsupported message type for admin REPLACE_MESSAGE handler: " + msgType.intValue();
throw new BrokerException(emsg, Status.BAD_REQUEST);
}
return (ba);
}
Aggregations