J2EE : JMS
IMPORTS
|
· import javax.jms.Connection; // con = connectionFactory.createConnection() 2
· import javax.jms.ConnectionFactory; //1
· import javax.jms.Destination;
· import javax.jms.JMSException;
· import javax.jms.Message;
· import javax.jms.MessageConsumer; con.createConsumer();
· import javax.jms.MessageListener;
· import javax.jms.MessageProducer; session.createProducer
· import javax.jms.Session; session = con.createSession(); //3 connection.start();
|
CODE : registering message consumer
|
ConnectionFactory connectionFactory = (ConnectionFactory) JNDIUtils.getJNDIResouceSource(obtainValidResourceName(resourceName));
connection = connectionFactory.createConnection();
//STEP1: CREATE CONNECTION
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);//STEP2: CREATE SESSION
connection.start(); STEP3: START CONNECTION
MessageConsumer consumer = session.createConsumer((Destination)
STEP3: CREATE CONSUMER JNDIUtils.getJNDIResouceSource(obtainValidResourceName(destinationName)),selector);
consumer.setMessageListener(msgHandler);
STEP4: START CONNECTIO
|
Message Producer CODE
|
try {
MessageProducer producer=null;
Session session=null;
if(!producersAndSessionsCache.containsKey(resourceName)){
ConnectionFactory connectionFactory = (ConnectionFactory) JNDIUtils.getJNDIResouceSource(resourceName);
Connection connection = connectionFactory.createConnection();
//STEP1: CREATE CONNECTION
session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
//STEP2: CREATE SESSION
connection.start();/STEP3: START CONNECTION
producer = session.createProducer(null); STEP3: CREATE PRODUCER
producersAndSessionsCache.put(resourceName, producer); STEP3: CREATE PRODUCER
producersAndSessionsCache.put(resourceName+SESSIONS_KEY_SUFFIX, session);
}else{
producer=(MessageProducer)producersAndSessionsCache.get(resourceName);
session=(Session)producersAndSessionsCache.get(resourceName+SESSIONS_KEY_SUFFIX);
}
producer.send((Destination) JNDIUtils.getJNDIResouceSource(obtainValidResourceName(destinationName)),buildMessage(msg, msgType, session,props));
producer.send(TOPIC NAME, MESSAGE);
|
private static Message buildMessage(Serializable msg, int msgType, Session session,Map
Message message = null;
switch (msgType) {
case TEXT_MESSAGE:
message = session.createTextMessage((String) msg);
break;
case OBJECT_MESSAGE:
message = session.createObjectMessage(msg);
break;
}
if(message!=null && props!=null && props.size()>0){// set property values for selectors/filters
String propertyName;
Object value=null;
for(Map.Entry
{
propertyName= item.getKey();
value= item.getValue();
if(value!=null && value instanceof String){
message.setStringProperty(propertyName, (String)value);
}else if(value!=null && value instanceof Integer){
message.setIntProperty(propertyName, ((Integer)value).intValue());
}else if(value!=null && value instanceof Long){
message.setLongProperty(propertyName, ((Long)value).longValue());
}else if(value!=null && value instanceof Double){
message.setDoubleProperty(propertyName, ((Double)value).doubleValue());
}else if(value!=null && value instanceof Boolean){
message.setBooleanProperty(propertyName, ((Boolean)value).booleanValue());
}else if(value!=null && value instanceof Byte){
message.setByteProperty(propertyName, ((Byte)value).byteValue());
}else if(value!=null && value instanceof Float){
message.setFloatProperty(propertyName, ((Float)value).floatValue());
}else if(value!=null && value instanceof Short){
message.setShortProperty(propertyName, ((Short)value).shortValue());
}else{
Logger.warn(" publishing message property value type is not supported :"+propertyName, MessagingUtils.class);
}
}
}
return message;
}
|
-->
6. What are transaction options available in JMS?
|
6. What are transaction options available in JMS?
|
An application has myriad transaction options available, including whether or not it wants to participate in transactions. If your application does not use transactions, it can use one of these acknowledgement modes: auto, duplicates okay, and client. You specify the acknowledgement modes when creating a JMS session. If your application uses transactions, it can choose from these transaction options: transacted session, MDB with container-managed transaction demarcation (CMTD), and MDB with bean-managed transaction demarcation (BMTD). The following lists briefly describe these acknowledgement modes and transaction options.
Acknowledgement options:
·Auto mode: When a session uses auto mode, the messages sent or received from the session are automatically acknowledged. This is the simplest mode and expresses JMS's power by enabling once-only message delivery guarantee.
·Duplicates okay mode: When a session uses duplicates okay mode, the messages sent or received from the session are automatically acknowledged just like auto mode, albeit lazily. Under rare circumstances, the messages might be delivered more than once. This mode enables at-least-once message delivery guarantee.
·Client mode: When a session uses client mode, the messages sent or received from the session are not acknowledged automatically. The application must acknowledge the message receipt. This mode gives the application (rather than the JMS provider) complete control over message acknowledgement, at the cost of increased code complexity.
Other types of acknowledgement modes are possible. However, these acknowledgement modes are JMS provider specific, and therefore, compromise the JMS application portability.
Transaction options:
·Transacted session: An application can participate in a transaction by creating a transacted session (or local transaction). The application completely controls the message delivery by either committing or rolling back the session.
·Message-driven beans with CMTD: An MDB can participate in a container transaction by specifying CMTD in the XML deployment descriptor. The transaction commits upon successful message processing or the application can explicitly roll it back.
·Message-driven beans with BMTD: An MDB can choose not to participate in a container transaction by specifying BMTD in the XML deployment descriptor. The MDB programmer has to design and code programmatic transactions.
|
Load only shows reference & loads only when some property is accessed that too from cache
|
No comments:
Post a Comment