Home Education and learning Web hosting and design Small Business
Some easy way for mathematical calculation Display all tips       Hide all tips
Java code for sending messages (using JMS)
file name : HubWriter.java


package mypackage;

import javax.jms.QueueSession;
import javax.jms.Queue;
import javax.jms.QueueSender;
import javax.jms.TextMessage;
import javax.jms.JMSException;
import ApplicationException;


public class HubWriter {
private QueueSession queueSession;
private static QueueSender QueueSender;

/**
* Default Constructor
*
*/
public HubWriter(){

}
/**
* This is a parameterised Constructor for the HubWriter class.
*
* Method name HubWriter
*@param session This is a QueueSession object
*@param queue This is a Queue object
*@throws ApplicationException
*/
public HubWriter(QueueSession session, Queue queue)
throws ApplicationException {
try {
// Create a new queue sender using the queue session.
// The sender should be created to send messages to the queue q.
queueSession = session;
if (QueueSender == null){
QueueSender = queueSession.createSender(queue);
}
}
catch (JMSException ex) {
throw new ApplicationException("Failed to create Sender", ex);
}
}

/**
* Creates the TextMessage from the input data and send it to HUB.
*
* Method name writeMessage
* Logic The method recieves reference of HubMessage,
* create a text message
* and send to the message hub using queue sender instance.
* @param objHubMsg Message that need to be send to HUB
* @throws ApplicationException
*/
public void writeMessage(String objHubMsg) throws ApplicationException {
TextMessage messageToHub = null;
try {

// Creates the text message for the supplied data
messageToHub = queueSession.createTextMessage();
messageToHub.setText(objHubMsg);
QueueSender.send(messageToHub);
}
catch (JMSException ex) {
throw new ApplicationException(
"Failed to write message on the queue",
ex);
}
}

/**
* This gets the object of QueueSession for the HUB.
*
* Method name getQueueSession
* @return queueSession QueueSession object
*/
private QueueSession getQueueSession() {
return queueSession;
}

/**
* This gets the QueueSender for the Queue.
*
* Method name getQueueSender
* @return queueSender QueueSender object
*/
private QueueSender getQueueSender() {
return QueueSender;
}
}

file name : HubMessageManager.java

   


package mypackage;

import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueue;

import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import javax.jms.Queue;
import javax.jms.QueueSender;
import javax.jms.JMSException;

import ApplicationException;

import EnterpriseHubCommandInterface;

 
public class HubMessageManager implements EnterpriseHubCommandInterface {

    private String queueManager = "";
    private String queueHost = "";
    private String queuePort = "";
    private String queueName = "";

    private QueueConnection queueConnection = null;
    private QueueSession queueSession = null;
    private QueueSender queueSender = null;
    private Queue queue = null;
    private static HubMessageManager hubMessageManager = null;

    /**
     * 
     *Default constructor 
     */
    private HubMessageManager(){
    	
    }

    /**
     * This is a default Constructor for the HubMessageManager class
     * 
     * 
     * Method			HubMessageManager
     * Logic			Constructor for the class for Initialization of the
     * 				Instance variables.		
     * 
     * Exception		ApplicationException.
     */
    private  HubMessageManager(
        String hubMgr,
        String host,
        String port,
        String queue)
        throws ApplicationException {
        try {
            queueSender = null;
            queueManager = hubMgr;
            queueHost = host;
            queuePort = port;
            queueName = queue;
            setupMessageEnvironment();
        }
        catch (Exception ex) {
            throw new ApplicationException(
                "Failed to instantiate" + " the HubMessageManager",
                ex);
        }
    }

    /**
     * This method returns the existing instance of HubMessageManager (if present) or
     * creates a new one and returns it. 
     *
     * Method name		getMessageMgrInstance
     * Logic			It is a static factory method to implement singleton 
     * 					access to HubMessageManager instance.
     *  
     * @return			HubMessageManager Returns the instance of HubMessageManager.
     * Exception		ApplicationException.
     */
    public static HubMessageManager getMessageMgrInstance (
        	String hubMgr,
            String host,
            String port,
            String queue) throws ApplicationException
            {
                 if(hubMessageManager ==null) {
                 	
                 	hubMessageManager= new HubMessageManager(hubMgr, host, port, queue);
                 }
                 return hubMessageManager;
        }

    /**
     * This method sets the initial environment for the Messaging.
     *
     * Method name		setupMessageEnvironment()
     * Logic			The method initiates the connection to MQManager and 
     * 				the identified queue.Then it istarts the connection and
     *                  	establishes a queue session necessary for communication.
     * 
     * Exception		ApplicationException	
     */
    public void setupMessageEnvironment() throws ApplicationException {

        MQQueueConnectionFactory factory = new MQQueueConnectionFactory();
        MQQueue mqQueue = null;
        QueueSession tmpQueueSession = null;
        try {
            factory.setTransportType(1);
            factory.setQueueManager(queueManager);
            factory.setHostName(queueHost);
            factory.setPort(Integer.parseInt(queuePort));

            queueConnection = factory.createQueueConnection();
            queueConnection.start();
            tmpQueueSession = getHubSession();
            if (tmpQueueSession != null) {
                 if (queue == null) {
                    queue = tmpQueueSession.createQueue(queueName);
                    mqQueue = (MQQueue) queue;
                    mqQueue.setTargetClient(1);
                }
            }
        }
        catch (JMSException ex) {
            throw new ApplicationException(
                "Failed to set hub environment: ",
                ex);
        }
    }

    /**
     * 
     * @return	queue
     */
    public Queue getQueue() {
        return queue;
    }

    /**
     * This method gets the Hubsession and returns it to the calling routine.
     *
     * Method name		getHubSession
     * Logic			The method makes use of the connection created in 
     * 					setMessageEnvironement()
     * 					method and establishes a session with Message Hub.
     * Exception		ApplicationException
     */
    private QueueSession getHubSession() throws ApplicationException {
        try {
            if (queueSession != null)
                return queueSession;

            queueSession = queueConnection.createQueueSession(false, 1);
        }
        catch (JMSException ex) {
            throw new ApplicationException(
                "Failed to create hub session: ",
                ex);
        }
        return queueSession;
    }

    /**
     * This method builds the Message and Send it to the HUB.
     *  
     * Method name		sendMessage
     * Logic			The method recieves the message  as parameter and 
     * 				transforms it to a String.It then uses the Session by 
     * 				calling getHubSession() and establishes communication 
     * 				with the Message queue defined for this batch process.
     * 				Then it creates the HubMessage instnace using the message 
     * 				parameter and instance of hubWriter to write the message
     *				Then writeMessage method of hubwriter is called and 
     *                  	the message is posted onto the queue.
     * @param			XmlData		Data that need to be sebnd to HUB
     * Exception		JMSException
     */
    public void sendMessage(Object xmlData)
        throws JMSException, ApplicationException {
        if (xmlData != null) {
            HubWriter humMessageWriter =
                new HubWriter(getHubSession(), (MQQueue) getQueue());
            humMessageWriter.writeMessage(xmlData.toString());
        }
    }
}



  
   

file name : EnterpriseHubCommandInterface.java

   


package mypackage;
import javax.jms.JMSException;
import ApplicationException;

public interface EnterpriseHubCommandInterface {

    /**
     * This method is used to send the message to a particular Queue
     * on HUB.
     * 
     * Method 	sendMessage
     * @param	messageData		Message data that need to tbe send to HUB
     * 							It may be TextMessage,A collection like HashMap, 
     * 							A serialize Object etc.
     * @throws	JMSException
     * @throws	ApplicationException
     */
    void sendMessage(Object messageData)
        throws JMSException, ApplicationException;
}


  
   
file name : EnterpriseBatchProcessInterface.java
   package mypackage;

 

public interface EnterpriseBatchProcessInterface
{

	/**
	 * This method acts as a starting point fot the batch process.
	 * 
	 * Method 	start
	 */
	void start();
	
	/**
	 * This method used for the initialization of the resources
	 * 
	 * Method 	initialize
	 */
	void initialize(); 
}
   
   

file name : ApplicationException.java

   package mypackage;

public class ApplicationException extends Exception {
    
    private String errorDescription = "";
    private Exception specificException = null;

    /**
     * 
     *Default Constructor
     * 
     */
    public ApplicationException(){
    	
    }
    
    
    /**
     * Constructor
     * parameterized  Constructor for the class.
     * Initialization of the Instance variable.
     * 
     * @param cause         Exception occured
     * @param description   Description of the message
     */
    public ApplicationException(String description, Exception cause) {
    	super(description + cause);
        specificException = cause;
        errorDescription = description;
    }

    /**
     * Parameterized Constructor for the class.
     * Initialization of the Instance variable.
     * 
     * @param   errorMessage string
     */
    public ApplicationException(String errorMessage) {
        errorDescription = errorMessage;
    }

    /**
     * This method sets Error message string that need
     * to be entered in the log file
     * 
     * @param   description string.
     */
    public void setDescription(String description) {
        errorDescription = description;
    }

    /**
     * This method returns message string that need
     * to be entered in the log file
     * @return  Description 
     */
    public String getDescription() {
        return errorDescription;
    }


    /**
     * 
     * @return  specificException Specific-Exception
     */
    public Exception getSpecificException() {
        return specificException;
    }
}