Candibiotic
News Update :

English Premier League

Bundesliga

La Liga

Showing posts with label JMS Sample Code. Show all posts
Showing posts with label JMS Sample Code. Show all posts

JMS Sample Code

Wednesday, July 15, 2009

import java.util.Properties;
import java.util.ResourceBundle;

import javax.naming.*;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Destination;
import javax.jms.MessageProducer;


public class JMSClientJNDIImpl {

private static ConnectionFactory connectionFactory = null;
private static Destination destination = null;

protected JMSClientJNDIImpl() {

}

/**
* SingletonHolder is loaded on the first execution of
* Singleton.getInstance() or the first access to SingletonHolder.INSTANCE ,
* not before.
*/

private static class SingletonHolder {

private final static JMSClientJNDIImpl INSTANCE = new JMSClientJNDIImpl();

static {
Context jndiContext = null;


try {

jndiContext = new InitialContext();

/*
* Look up connection factory and destination. If either does not exist, exit.
* If you look up a TopicConnectionFactory or a QueueConnectionFactory,
* program behavior is the same.
*/
String destinationName = getBundle().getString("jms.connection.factory");
connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/MyConnectionFactory");
destination = (Destination) jndiContext.lookup(destinationName);

} catch (NamingException e) {
e.printStackTrace();

} catch (Exception e) {
e.printStackTrace();

}
}

}

//returns instance of JMSClientJNDIImpl
public static JMSClientJNDIImpl getInstance() {
return SingletonHolder.INSTANCE;
}

/**
* @param transactionID
* @param workFlowID
* @param messageTitle
* @param timestamp
* @param transformFlag
*/
public void transactionMessagesLogging(String transactionID,
String xmlData, String messageTitle, String timestamp,
Boolean transformFlag) throws Exception {
try {
StringBuffer loggingMessage = new StringBuffer();
createAndSendMessage(msg);

} catch (JMSException e) {


} catch (Exception e) {
e.printStackTrace();


}

}

/**
* this method uses the connection to create a session and messages to the queue
* @param transactionID
* @param xmlData
* @param messageTitle
* @param timestamp
* @param transformFlag
* @param loggingMessage
* @throws JMSException
* @throws Exception
*/
private void createAndSendMessage(String msg) throws JMSException, Exception {

Connection connection = null;
MessageProducer producer = null;
Session session = null;


try {
/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create producer and text message.
* Send messages, varying text slightly.
* Send end-of-messages message.
* Finally, close connection.
*/
connection = connectionFactory.createConnection();

session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination);


//creating the text message
TextMessage transactionMessage = session.createTextMessage();
loggingMessage.append(msg.trim());
transactionMessage.setText(loggingMessage.toString());
producer.send(transactionMessage);

/*
* Send a non-text control message indicating end of
* messages.
*/
producer.send(session.createMessage());

} catch (JMSException e) {
e.printStackTrace();


} catch (Exception e) {
e.printStackTrace();


} finally {
try {
// closing resources
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {


}
}
}

//read values from properties file
private static ResourceBundle getBundle() throws Exception {
MessageReader reader = MessageReader.getInstance();
ResourceBundle bundle = reader.getReader("ApplicationResources");
return bundle;
}


}

Football News

 

© Copyright Vinayak Wins 2010 -2011 | Design by Herdiansyah Hamzah | Published by Borneo Templates | Powered by Blogger.com.