📄 enqueueprdhelper.java
字号:
package oracle.otnsamples.AQ.Helper;
/**
* @author Anupama
* @version 1.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the Application : EnqueuePrdHelper.java
* Creation/Modification History :
*
* Anupama 29-Jan-2002 Created
*
*/
// Oracle JMS imports
import oracle.jms.AdtMessage;
import oracle.jms.AQjmsFactory;
import oracle.jms.AQjmsConstants;
import oracle.jms.AQjmsSession;
// JMS imports
import javax.jms.DeliveryMode;
import javax.jms.TopicConnection;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnectionFactory;
import javax.jms.JMSException;
import javax.jms.TopicSession;
import javax.jms.TopicPublisher;
// SQL imports
import java.sql.SQLException;
// Util imports
import java.util.Hashtable;
/**
* This class is used to enqueue the product details sent to it by creating
* a connection with the database using JMS classes and then publishing
* the message. Before running this class make sure that these are done.
* 1. Queue table is created in the database.
* 2. At least one subscriber is created for the queue.
* 3. The queue is started.
*/
public class EnqueuePrdHelper{
/**
* This method is called after a product is successfully added or
* updated in the database. The main tasks performed by this
* method is call the methods to create a session, send the session
* thus retrieved to get the topic and use this topic to enqueue the
* message.
*
* @param p_addedProductInfo Details of the product to be added to queue
* @param p_user Type of the user
* @param p_action It tells if the product is being added or updated
* @exception Exception Incase of some error or unreported Exception.
*/
public static void addPrdToQueue(Product p_addedProductInfo, String p_user,
String p_action) throws Exception {
// Get the Database userDetails using the Connection Helper.
Hashtable dbUserDetails = ConnectionHelper.getDBUserDetails(p_user);
// Connects to the database and creates Topic Session
Session session = createJmsSession(dbUserDetails);
// Gets the topic for the queue present in database.
Topic topic = getProductsTopic(session, dbUserDetails);
// Publishes the product after storing the details of the product in the
// object of ProductDat.
publishManufacturerPrd(session, topic, p_addedProductInfo, p_user, p_action);
}
/**
* This method is called after the products selected are successfully
* deleted from the database. The main tasks performed by this
* method is call the methods to create a session, send the session
* thus retrieved to get the topic and use this topic to enqueue all
* the
* products message.
*
* @param p_prdsDeletedArr Details of the product to be enqueued which are
* deleted from the database
* @param p_user Type of the user
* @param p_action It tells if the product is being added or updated or
* deleted
* @exception Exception Incase of some error or unreported Exception.
*/
public static void addPrdToQueue(Product[] p_prdsDeletedArr, String p_user,
String p_action) throws Exception {
// Get the Database userDetails using the Connection Helper.
Hashtable dbUserDetails = ConnectionHelper.getDBUserDetails(p_user);
// Connects to the database and creates Topic Session
Session session = createJmsSession(dbUserDetails);
// Gets the topic for the queue present in database.
Topic topic = getProductsTopic(session, dbUserDetails);
// Publishes the product after storing the details of the product in the
// object of ProductDat.
publishManufacturerPrd(session, topic, p_prdsDeletedArr, p_user, p_action);
}
/**
* This method helps to create a JMSSession object.
* The JMSSession object is used to create a session
* object through which the publisher and subscribers
* can send and receive messages using queue.
* Depending on the type of user, the parameters of the
* database are retrieved.
*
* @exception Exception Incase of some error or unreported Exception.
* @param dbUserDetails Contains the database user to which the
* manufacturer should connect
* @return Returns the Session created through which messages can be
* published.
*/
public static Session createJmsSession(Hashtable p_dbUserDetails)
throws Exception{
try{
TopicConnectionFactory topicConnFactory;
TopicConnection topicConnection;
TopicSession topicSession = null;
String hostName = (String)p_dbUserDetails.get("HostName");
String SID = (String)p_dbUserDetails.get("SID");
int portNo = Integer.parseInt(((Integer)p_dbUserDetails.
get("PortNo")).toString());
String driver = (String)p_dbUserDetails.get("Driver");
String userName = (String)p_dbUserDetails.get("UserName");
String password = (String)p_dbUserDetails.get("Password");
// Connection Factory is required to tell to which database we are
// about to connect, its host number and other details.
// A Topic Connection Factory is required if there are multiple subscriber
// to the queue. The static method getTopicConnectionFactory present
// in AQjmsFactory retrieves Connection Factory.
// The syntax of the method is
// getTopicConnectionFactory(String hostname,String oracle_sid,
// int port,String driver)
topicConnFactory=AQjmsFactory.getTopicConnectionFactory(hostName, SID,
portNo, driver);
// A Topic Connection must be created to the database where the queue
// resides. It is retrieved using the method createTopicConnection
// present in Topic Connection Factory.
// The syntax of this method used here is
// createTopicConnection(String userName,String password)
topicConnection = topicConnFactory.createTopicConnection(userName,password);
// A topic session is required for publishing the messages. Destination
// objects are created from a Session object using domain
// specific session methods.
// The syntax of this method is
// createTopicSession(boolean newSession, int properties)
// where newSession tells if a new JMS Session need to be created
// properties tells the properties of the Session like
// if acknowledgement is required or not, etc
try{
topicSession = topicConnection.createTopicSession(true, 0);
}catch(Exception e){
e.printStackTrace();
}
// Start the connection to publish messages
topicConnection.start();
return topicSession;
}catch(JMSException p_JmsExp){
p_JmsExp.printStackTrace();
throw new Exception("AQ-1013");
}
}
/**
* This method helps to retrieve the topic for the queue table
* specified in the respective database. Topic is used to publish
* messages to multi consumer queue.
*
* @param jmsSession Session object in which this topic is created
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -