Skip to main content




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 props) throws Exception {
                        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 item:props.entrySet())
                           {
                              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

Comments

Popular posts from this blog

Microservices Design patterns

What are microservices? Microservices - also known as the microservice architecture - is an architectural style that structures an application as a collection of services that are Highly maintainable and testable Loosely coupled Independently deployable Organized around business capabilities Owned by a small team The microservice architecture enables the rapid, frequent and reliable delivery of large, complex applications. It also enables an organization to evolve its technology stack. You are developing a server-side enterprise application. It must support a variety of different clients including desktop browsers, mobile browsers and native mobile applications. The application might also expose an API for 3rd parties to consume. It might also integrate with other applications via either web services or a message broker. The application handles requests (HTTP requests and messages) by executing business logic; accessing a database; exchanging messages with other systems; and returni...

GraphQL

What is GraphQL  API Standard invented & open-sourced by Facebook Alternative to  REST API  enables declarative data fetching  exposes single endpoint & responds to queries How it works?  Why Graphql? Improvises performance by reducing the data that is to be transferred over the internet Variety of different frontend frameworks and platforms on client-side Fast development speed & expectation for rapid feature development Why Graphql is better than REST? Flexibility & efficient  No more over /under fetching of data Over fetching : Under fetching: Insightful analytics  Schema serves as contract between client and server CORE CONCEPTS : SDL :SCHEMA DEFINITION LANGUAGE Writing Data with mutations 3 kinds of mutations creating new data updating existing data deleting existing data

Jackson

<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> jackson-core </artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> jackson-annotations </artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> jackson-databind </artifactId> <version>2.9.6</version> </dependency> CBOR encoded data with Jackson <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-cbor</artifactId> <version>2.9.6</version> </dependency> In order to read and write MessagePack encoded data <dependency> <groupId>org.msgpack</groupId> <artifactId>jackson-dataformat-msgp...