use of javax.jms.JMSException in project carbon-apimgt by wso2.
the class BrokerUtil method publishToTopic.
/**
* Publish to broker topic
*
* @param topicName publishing topic name
* @param gatewayEvent topic message data object
*/
public static void publishToTopic(String topicName, GatewayEvent gatewayEvent) throws GatewayException {
TopicSession topicSession = null;
Topic topic = null;
TopicPublisher topicPublisher = null;
TopicConnection topicConnection = null;
try {
topicConnection = getTopicConnection();
topicConnection.start();
topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = topicSession.createTopic(topicName);
topicPublisher = topicSession.createPublisher(topic);
TextMessage textMessage = topicSession.createTextMessage(new Gson().toJson(gatewayEvent));
topicPublisher.publish(textMessage);
} catch (JMSException e) {
String errorMessage = "Error occurred while publishing " + gatewayEvent.getEventType() + " event to JMS " + "topic :" + topicName;
log.error(errorMessage, e);
throw new GatewayException(errorMessage, ExceptionCodes.GATEWAY_EXCEPTION);
} catch (BrokerException e) {
String errorMessage = "Error occurred while obtaining broker topic connection for topic : " + topicName;
log.error(errorMessage, e);
throw new GatewayException(errorMessage, ExceptionCodes.GATEWAY_EXCEPTION);
} finally {
if (topicPublisher != null) {
try {
topicPublisher.close();
} catch (JMSException e) {
log.error("Error occurred while closing topic publisher for topic : " + topicName);
}
}
if (topicSession != null) {
try {
topicSession.close();
} catch (JMSException e) {
log.error("Error occurred while closing topic session for topic : " + topicName);
}
}
if (topicConnection != null) {
try {
topicConnection.close();
} catch (JMSException e) {
log.error("Error occurred while closing topic connection for topic : " + topicName);
}
}
}
}
use of javax.jms.JMSException in project wso2-synapse by wso2.
the class JmsConsumer method cleanup.
public boolean cleanup() throws SynapseException {
if (logger.isDebugEnabled()) {
logger.debug(getId() + " cleaning up...");
}
try {
store.cleanup(connection, session);
return true;
} catch (JMSException e) {
throw new SynapseException("Error while connecting to store to close created connections. JMS provider " + "might not be accessible" + store.getName(), e);
} finally {
connection = null;
session = null;
consumer = null;
}
}
use of javax.jms.JMSException in project wso2-synapse by wso2.
the class JmsConsumer method receive.
public MessageContext receive() {
boolean connectionSuccess = checkAndTryConnect();
if (!connectionSuccess) {
throw new SynapseException(idString + "Error while connecting to JMS provider. " + MessageProcessorConstants.STORE_CONNECTION_ERROR);
}
try {
Message message = consumer.receive(1);
if (message == null) {
return null;
}
if (!(message instanceof ObjectMessage)) {
logger.warn("JMS Consumer " + getId() + " did not receive a javax.jms.ObjectMessage");
// we just discard this message as we only store Object messages via JMS Message store
message.acknowledge();
return null;
}
ObjectMessage msg = (ObjectMessage) message;
String messageId = msg.getStringProperty(Constants.OriginalMessageID);
if (!(msg.getObject() instanceof StorableMessage)) {
logger.warn("JMS Consumer " + getId() + " did not receive a valid message.");
message.acknowledge();
return null;
}
// create a ,essage context back from the stored message
StorableMessage storableMessage = (StorableMessage) msg.getObject();
org.apache.axis2.context.MessageContext axis2Mc = store.newAxis2Mc();
MessageContext synapseMc = store.newSynapseMc(axis2Mc);
synapseMc = MessageConverter.toMessageContext(storableMessage, axis2Mc, synapseMc);
// cache the message
updateCache(message, synapseMc, messageId, false);
if (logger.isDebugEnabled()) {
logger.debug(getId() + " Received MessageId:" + messageId + " priority:" + message.getJMSPriority());
}
return synapseMc;
} catch (JMSException e) {
logger.error("Cannot fetch messages from Store " + store.getName());
updateCache(null, null, "", true);
cleanup();
/* try connecting and receiving again. Try to connect will happen configured number of times
and give up with a SynapseException */
return receive();
}
}
use of javax.jms.JMSException in project wso2-synapse by wso2.
the class JmsStore method getProducer.
public MessageProducer getProducer() {
if (cacheLevel == 1 && cachedProducer != null) {
return cachedProducer;
}
if (this.producer == null) {
this.producer = new JmsProducer(this);
this.producer.setId(nextProducerId());
} else {
return producer;
}
Session session = null;
javax.jms.MessageProducer messageProducer;
try {
synchronized (producerLock) {
if (producerConnection == null) {
newWriteConnection();
}
}
if (((JmsProducer) this.producer).getSession() == null) {
try {
session = newSession(producerConnection(), Session.AUTO_ACKNOWLEDGE, true);
} catch (JMSException e) {
newWriteConnection();
session = newSession(producerConnection(), Session.AUTO_ACKNOWLEDGE, true);
}
messageProducer = newProducer(session);
((JmsProducer) this.producer).setConnection(producerConnection()).setSession(session).setProducer(messageProducer);
if (logger.isDebugEnabled()) {
logger.debug(nameString() + " created message producer " + this.producer.getId());
}
if (cacheLevel == 1) {
cachedProducer = producer;
}
}
} catch (Throwable t) {
String errorMsg = "Could not create a Message Producer for " + nameString();
logger.error(errorMsg, t);
synchronized (producerLock) {
try {
cleanup(producerConnection, session);
} catch (JMSException e) {
throw new SynapseException("Error while cleaning up connection for message store " + nameString(), e);
}
producerConnection = null;
}
}
return this.producer;
}
use of javax.jms.JMSException in project wso2-synapse by wso2.
the class JmsStore method getDestination.
/**
* Get Destination denoted by destination variable by looking up context or
* creating it using the session.
*
* @param session Session to create destination from
* @return Destination object
* @throws JMSException on a JMS exception when creating Destination using session
* @throws StoreForwardException on other issue
*/
private Destination getDestination(Session session) throws JMSException, StoreForwardException {
Destination dest = queue;
if (dest != null) {
return dest;
}
// try creating a destination by looking up context
InitialContext newContext;
String destinationLookupFailureReason = "";
try {
dest = lookup(context, javax.jms.Destination.class, destination);
} catch (NamingException e) {
if (logger.isDebugEnabled()) {
logger.debug(nameString() + ". Could not lookup destination [" + destination + "]. Message: " + e.getLocalizedMessage());
}
// try to re-init the context
newContext = newContext();
try {
dest = lookup(newContext, Destination.class, destination);
} catch (Throwable t) {
destinationLookupFailureReason = nameString() + ". Destination [" + destination + "] not defined in JNDI context. Message:" + t.getLocalizedMessage();
}
}
// try creating destination by session as lookup failed (dest == null)
if (dest == null) {
if (session == null) {
throw new StoreForwardException(nameString() + "cannot create Destination" + destination + ". JMS Session " + "cannot be null");
}
try {
dest = session.createQueue(destination);
if (logger.isDebugEnabled()) {
logger.debug(nameString() + " created destination [" + destination + "] from session object.");
}
} catch (JMSException e) {
String error = nameString() + " could not create destination [" + destination + "]. from session or by JNDI context lookup. ";
error.concat("create by session error: " + e);
if (!destinationLookupFailureReason.isEmpty()) {
error.concat(" create by lookup error: " + destinationLookupFailureReason);
}
throw new StoreForwardException(error, e);
}
}
synchronized (queueLock) {
queue = dest;
}
return dest;
}
Aggregations