use of com.swiftmq.jms.MessageImpl in project swiftmq-client by iitsoftware.
the class MessageConsumerImpl method receiveMessage.
synchronized Message receiveMessage(boolean block, long timeout) throws JMSException {
verifyState();
if (messageListener != null) {
throw new JMSException("receive not allowed while a message listener has been set");
}
try {
if (!consumerStarted)
fillCache();
if (messageCache.getSize() == 0) {
if (block) {
receiverWaiting = true;
if (timeout == 0)
wait();
else
wait(timeout);
} else {
if (fillCachePending && receiveNoWaitFirstCall) {
wait(1000);
}
}
}
} catch (InterruptedException e) {
}
receiverWaiting = false;
if (messageCache.getSize() == 0 || isClosed())
return null;
AsyncMessageDeliveryRequest request = (AsyncMessageDeliveryRequest) messageCache.remove();
MessageEntry messageEntry = request.getMessageEntry();
MessageImpl msg = messageEntry.getMessage();
messageEntry.moveMessageAttributes();
msg.setMessageConsumerImpl(this);
msg.reset();
msg.setReadOnly(true);
msg.setUseThreadContextCL(useThreadContextCL);
if (request.isRequiresRestart())
fillCache();
if (reportDelivered)
reportDelivered(msg);
if (doAck) {
try {
acknowledgeMessage(msg.getMessageIndex(), false);
} catch (JMSException e) {
}
}
return msg;
}
use of com.swiftmq.jms.MessageImpl in project swiftmq-client by iitsoftware.
the class MessageConsumerImpl method invokeMessageListener.
private void invokeMessageListener() {
if (isClosed())
return;
AsyncMessageDeliveryRequest request = (AsyncMessageDeliveryRequest) messageCache.remove();
MessageEntry messageEntry = request.getMessageEntry();
MessageImpl msg = messageEntry.getMessage();
messageEntry.moveMessageAttributes();
MessageIndex msgIndex = msg.getMessageIndex();
msg.setMessageConsumerImpl(this);
try {
msg.reset();
} catch (JMSException e) {
e.printStackTrace();
}
msg.setReadOnly(true);
msg.setUseThreadContextCL(useThreadContextCL);
if (reportDelivered)
reportDelivered(msg);
try {
messageListener.onMessage(msg);
} catch (RuntimeException e) {
System.err.println("ERROR! MessageListener throws RuntimeException, shutting down consumer!");
e.printStackTrace();
try {
close(e.toString());
} catch (JMSException e1) {
}
return;
}
if (!wasRecovered) {
if (request.isRequiresRestart())
fillCache();
if (doAck) {
try {
acknowledgeMessage(msgIndex, false);
} catch (JMSException e) {
}
}
} else
wasRecovered = false;
}
use of com.swiftmq.jms.MessageImpl in project swiftmq-ce by iitsoftware.
the class NontransactedTopicSession method visitProduceMessageRequest.
public void visitProduceMessageRequest(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();
MessageImpl msg = req.getMessage();
int producerId = req.getQueueProducerId();
Producer producer = null;
try {
if (producerId == -1) {
TopicImpl topic = (TopicImpl) msg.getJMSDestination();
if (topic.getType() != DestinationFactory.TYPE_TEMPTOPIC)
ctx.authSwiftlet.verifyTopicSenderSubscription(topic.getTopicName(), ctx.activeLogin.getLoginId());
producer = new TopicProducer(ctx, topic);
} 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 instanceof JMSException) ? e : new javax.jms.JMSException(e.toString()));
}
}
if (req.isReplyRequired())
reply.send();
}
use of com.swiftmq.jms.MessageImpl in project swiftmq-ce by iitsoftware.
the class TransactedQueueSession method visitCommitRequest.
public void visitCommitRequest(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
Object[] wrapper = req.getMessages();
ctx.incMsgsSent(wrapper.length);
req.setMessages(null);
long fcDelay = 0;
RingBuffer tempProducers = null;
for (int i = 0; i < wrapper.length; i++) {
DataByteArrayInputStream dis = new DataByteArrayInputStream((byte[]) wrapper[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 = (QueuePushTransaction) 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 instanceof JMSException) ? e : new javax.jms.JMSException(e.toString()));
}
reply.send();
}
use of com.swiftmq.jms.MessageImpl in project swiftmq-ce by iitsoftware.
the class TransactedQueueSession method visitCommitRequest.
public void visitCommitRequest(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
Object[] wrapper = req.getMessages();
ctx.incMsgsSent(wrapper.length);
req.setMessages(null);
long fcDelay = 0;
RingBuffer tempProducers = null;
for (int i = 0; i < wrapper.length; i++) {
DataByteArrayInputStream dis = new DataByteArrayInputStream((byte[]) wrapper[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 = (QueuePushTransaction) 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 instanceof JMSException) ? e : new javax.jms.JMSException(e.toString()));
}
reply.send();
}
Aggregations