use of com.swiftmq.jms.QueueImpl in project swiftmq-ce by iitsoftware.
the class TransactedQueueSession method visit.
public void visit(CommitRequest req) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/visitCommitRequest");
CommitReply reply = (CommitReply) req.createReply();
reply.setOk(true);
try {
// first: produce all messages
List ml = req.getMessages();
ctx.incMsgsSent(ml.size());
long fcDelay = 0;
RingBuffer tempProducers = null;
for (int i = 0; i < ml.size(); i++) {
DataByteArrayInputStream dis = new DataByteArrayInputStream((byte[]) ml.get(i));
int producerId = dis.readInt();
int type = dis.readInt();
MessageImpl msg = MessageImpl.createInstance(type);
msg.readContent(dis);
dis.close();
Producer producer = null;
if (producerId == -1) {
String queueName = ((QueueImpl) msg.getJMSDestination()).getQueueName();
if (!ctx.queueManager.isQueueRunning(queueName))
throw new InvalidDestinationException("Invalid destination: " + queueName);
producer = new QueueProducer(ctx, queueName);
if (tempProducers == null)
tempProducers = new RingBuffer(8);
tempProducers.add(producer);
transactionManager.addTransactionFactory(producer);
} else {
producer = (Producer) producerList.get(producerId);
}
QueuePushTransaction transaction = producer.getTransaction();
transaction.putMessage(msg);
fcDelay = Math.max(fcDelay, transaction.getFlowControlDelay());
if (producerId == -1)
producer.markForClose();
}
// Next: do the commit
transactionManager.commit();
if (tempProducers != null) {
int size = tempProducers.getSize();
for (int i = 0; i < size; i++) {
((Producer) tempProducers.remove()).close();
}
}
reply.setDelay(fcDelay);
purgeMarkedProducers();
purgeMarkedConsumers();
} catch (Exception e) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/commit produced messages failed: " + e.getMessage());
reply.setOk(false);
reply.setException(e);
}
reply.send();
}
use of com.swiftmq.jms.QueueImpl in project swiftmq-ce by iitsoftware.
the class NontransactedQueueSession method visit.
public void visit(ProduceMessageRequest req) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/visitProduceMessageRequest");
ctx.incMsgsSent(1);
ProduceMessageReply reply = null;
if (req.isReplyRequired())
reply = (ProduceMessageReply) req.createReply();
int producerId = req.getQueueProducerId();
Producer producer = null;
try {
MessageImpl msg = SMQPUtil.getMessage(req);
if (producerId == -1) {
String queueName = ((QueueImpl) msg.getJMSDestination()).getQueueName();
if (!ctx.queueManager.isQueueRunning(queueName))
throw new InvalidDestinationException("Invalid destination: " + queueName);
producer = new QueueProducer(ctx, queueName);
} else {
producer = (Producer) producerList.get(producerId);
}
QueuePushTransaction transaction = (QueuePushTransaction) producer.createTransaction();
transaction.putMessage(msg);
transaction.commit();
if (req.isReplyRequired()) {
reply.setDelay(transaction.getFlowControlDelay());
reply.setOk(true);
}
if (producerId == -1)
producer.close();
} catch (Exception e) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/produce messages failed: " + e.getMessage());
if (req.isReplyRequired()) {
reply.setOk(false);
reply.setException(e);
}
}
if (req.isReplyRequired())
reply.send();
}
use of com.swiftmq.jms.QueueImpl in project swiftmq-ce by iitsoftware.
the class NontransactedQueueSession method visit.
public void visit(ProduceMessageRequest req) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/visitProduceMessageRequest");
ctx.incMsgsSent(1);
ProduceMessageReply reply = null;
if (req.isReplyRequired())
reply = (ProduceMessageReply) req.createReply();
int producerId = req.getQueueProducerId();
Producer producer = null;
try {
MessageImpl msg = SMQPUtil.getMessage(req);
long ttl = msg.getJMSExpiration();
if (ttl > 0)
msg.setJMSExpiration(System.currentTimeMillis() + ttl);
if (producerId == -1) {
String queueName = ((QueueImpl) msg.getJMSDestination()).getQueueName();
if (!ctx.queueManager.isQueueRunning(queueName))
throw new InvalidDestinationException("Invalid destination: " + queueName);
producer = new QueueProducer(ctx, queueName);
} else {
producer = (Producer) producerList.get(producerId);
}
QueuePushTransaction transaction = (QueuePushTransaction) producer.createTransaction();
transaction.putMessage(msg);
transaction.commit();
if (req.isReplyRequired()) {
reply.setDelay(transaction.getFlowControlDelay());
reply.setOk(true);
}
if (producerId == -1)
producer.close();
} catch (Exception e) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/produce messages failed: " + e.getMessage());
if (req.isReplyRequired()) {
reply.setOk(false);
reply.setException(e);
}
}
if (req.isReplyRequired())
reply.send();
}
use of com.swiftmq.jms.QueueImpl in project swiftmq-ce by iitsoftware.
the class NontransactedQueueSession method visit.
public void visit(CreateConsumerRequest req) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/visitCreateConsumerRequest");
CreateConsumerReply reply = (CreateConsumerReply) req.createReply();
try {
ctx.activeLogin.getResourceLimitGroup().incConsumers();
} catch (ResourceLimitException e) {
reply.setOk(false);
reply.setException(new JMSException(e.toString()));
reply.send();
return;
}
QueueImpl queue = req.getQueue();
String messageSelector = req.getMessageSelector();
String queueName = null;
try {
queueName = queue.getQueueName();
} catch (JMSException ignored) {
}
try {
if (!queueName.endsWith('@' + SwiftletManager.getInstance().getRouterName()))
throw new InvalidDestinationException("Queue '" + queueName + "' is not local! Can't create a Consumer on it!");
int consumerId = 0;
QueueConsumer consumer = null;
consumerId = ArrayListTool.setFirstFreeOrExpand(consumerList, null);
consumer = new QueueConsumer(ctx, queueName, messageSelector);
consumerList.set(consumerId, consumer);
consumer.createReadTransaction();
consumer.createTransaction();
reply.setOk(true);
reply.setQueueConsumerId(consumerId);
if (receiverEntityList != null) {
Entity consEntity = receiverEntityList.createEntity();
consEntity.setName(queueName + "-" + consumerId);
consEntity.setDynamicObject(consumer);
consEntity.createCommands();
Property prop = consEntity.getProperty("queue");
prop.setValue(queueName);
prop.setReadOnly(true);
prop = consEntity.getProperty("selector");
if (messageSelector != null) {
prop.setValue(messageSelector);
}
prop.setReadOnly(true);
receiverEntityList.addEntity(consEntity);
}
} catch (InvalidSelectorException e) {
ctx.activeLogin.getResourceLimitGroup().decConsumers();
ctx.logSwiftlet.logWarning("sys$jms", ctx.tracePrefix + "/CreateConsumer has invalid Selector: " + e);
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/CreateConsumer has invalid Selector: " + e);
reply.setOk(false);
reply.setException(e);
} catch (Exception e1) {
ctx.activeLogin.getResourceLimitGroup().decConsumers();
ctx.logSwiftlet.logWarning("sys$jms", ctx.tracePrefix + "/Exception during create consumer: " + e1);
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/Exception during create consumer: " + e1);
reply.setOk(false);
reply.setException(e1);
}
reply.send();
}
use of com.swiftmq.jms.QueueImpl in project swiftmq-ce by iitsoftware.
the class HeuristicHandler method entityToMessage.
private MessageImpl entityToMessage(Entity entity) throws Exception {
XidImpl xid = (XidImpl) entity.getUserObject();
DataByteArrayOutputStream dos = new DataByteArrayOutputStream();
xid.writeContent(dos);
BytesMessageImpl message = new BytesMessageImpl();
message.setJMSDestination(new QueueImpl(HEURISTIC_QUEUE));
message.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
message.setJMSPriority(Message.DEFAULT_PRIORITY);
message.setJMSExpiration(Message.DEFAULT_TIME_TO_LIVE);
message.setJMSMessageID(MSGID + IdGenerator.getInstance().nextId('/'));
message.setStringProperty(PROP_OPER, (String) entity.getProperty(PROP_OPER).getValue());
message.setStringProperty(PROP_SID, entity.getName());
message.setIntProperty(PROP_IID, Integer.parseInt(entity.getName()));
message.writeBytes(dos.getBuffer(), 0, dos.getCount());
return message;
}
Aggregations