Enterprise Java Jan-June 2014 Prepared by: Mr. Hitesh Kumar Sharma & Mr. Ravi Tomar Page 1 Prepared for : B.Tech CS VI Sem (MFT+O&G+OSS+CCVT) Unit 6: Overview: JNDI, JMS, JPA, XML Introduction of JNDI Introduction to JMS, JMS Configuration Introduction of JPA & XML Packaging and Deploying J2EE application
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Enterprise Java Jan-June 2014
Prepared by: Mr. Hitesh Kumar Sharma & Mr. Ravi Tomar Page 1 Prepared for : B.Tech CS VI Sem (MFT+O&G+OSS+CCVT)
Unit 6: Overview: JNDI, JMS, JPA, XML
Introduction of JNDI
Introduction to JMS, JMS Configuration
Introduction of JPA & XML
Packaging and Deploying J2EE application
Enterprise Java Jan-June 2014
Prepared by: Mr. Hitesh Kumar Sharma & Mr. Ravi Tomar Page 2 Prepared for : B.Tech CS VI Sem (MFT+O&G+OSS+CCVT)
1.0. JNDI OVERVIEW
The Java Naming and Directory Interface (JNDI) is an application programming interface (API) that provides naming and directory functionality to applications written using the Java programming language. It is defined to be independent of any specific directory service implementation. Thus a variety of directories new, emerging, and already deployed can be accessed in a common way.
Architecture The JNDI architecture consists of an API and a service provider interface (SPI). Java applications use the JNDI API to access a variety of naming and directory services. The SPI enables a variety of naming and directory services to be plugged in transparently, thereby allowing the Java application using the JNDI API to access their services. See the following figure.
Packaging The JNDI is included in the Java 2 SDK, v1.3 and later releases. It is also available as a Java Standard Extension for use with the JDK 1.1 and the Java 2 SDK, v1.2. It extends the v1.1 and v1.2 platforms to provide naming and directory functionality.
Enterprise Java Jan-June 2014
Prepared by: Mr. Hitesh Kumar Sharma & Mr. Ravi Tomar Page 3 Prepared for : B.Tech CS VI Sem (MFT+O&G+OSS+CCVT)
To use the JNDI, you must have the JNDI classes and one or more service providers. The Java 2 SDK, v1.3 includes three service providers for the following naming/directory services:
Lightweight Directory Access Protocol (LDAP) Common Object Request Broker Architecture (CORBA) Common Object
Services (COS) name service Java Remote Method Invocation (RMI) Registry
Other service providers can be downloaded from the JNDI Web site or obtained from other vendors. When using the JNDI as a Standard Extension on the JDK 1.1 and Java 2 SDK, v1.2, you must first download the JNDI classes and one or more service providers. See the Preparations lesson for details on how to install the JNDI classes and service providers.
The javax.naming package contains classes and interfaces for accessing naming services.
Context
The javax.naming package defines a Context interface, which is the core interface for looking up, binding/unbinding, renaming objects and creating and destroying subcontexts. The most commonly used operation is lookup(). You supply lookup() the name of the object you want to look up, and it returns the object bound to that name. For example, the following code fragment looks up a printer and sends a document to the printer object to be printed.
Prepared by: Mr. Hitesh Kumar Sharma & Mr. Ravi Tomar Page 4 Prepared for : B.Tech CS VI Sem (MFT+O&G+OSS+CCVT)
Names
Every naming method in the Context interface has two overloads: one that accepts a Name argument and one that accepts a java.lang.String name. Name is an interface that represents a generic name--an ordered sequence of zero or more components. For the methods in the Context interface, a Name argument that is an instance of CompositeName represents a composite name , so you can name an object using a name that spans multiple namespaces. A Name argument of any other type represents a compound name. (Names are covered in the Beyond the Basics trail.) The overloads that accept Name are useful for applications that need to manipulate names, that is, composing them, comparing components, and so on.
A java.lang.String name argument represents a composite name. The overloads that accept java.lang.String names are likely to be more useful for simple applications, such as those that simply read in a name and look up the corresponding object.
Bindings listBindings() returns an enumeration of name-to-object bindings. Each binding is represented by an instance of the Binding class. A binding is a tuple containing the name of the bound object, the name of the object's class, and the object itself. list() is similar to listBindings(), except that it returns an enumeration of NameClassPair. NameClassPair contains an object's name and the name of the object's class. list() is useful for applications such as browsers that want to discover information about the objects bound within a context but that don't need all of the actual objects. Although listBindings() provides all of the same information, it is potentially a much more expensive operation.
References Objects are stored in naming and directory services in different ways. A service that supports storing Java objects might support storing an object in its serialized form. However, some naming and directory services do not support the storing of Java objects. Furthermore, for some objects in the directory, Java programs are but one group of applications that access them. In this case, a serialized Java object might not be the most appropriate representation. A reference might be a very compact representation of an object, whereas its serialized form might contain a lot more state.
Enterprise Java Jan-June 2014
Prepared by: Mr. Hitesh Kumar Sharma & Mr. Ravi Tomar Page 5 Prepared for : B.Tech CS VI Sem (MFT+O&G+OSS+CCVT)
The JNDI defines the Reference class to represent a reference. A reference contains information on how to construct a copy of the object. The JNDI will attempt to turn references looked up from the directory into the Java objects that they represent so that JNDI clients have the illusion that what is stored in the directory are Java objects.
The Initial Context In the JNDI, all naming and directory operations are performed relative to a context. There are no absolute roots. Therefore the JNDI defines an initial context, InitialContext, which provides a starting point for naming and directory operations. Once you have an initial context, you can use it to look up other contexts and objects.
Exceptions The JNDI defines a class hierarchy for exceptions that can be thrown in the course of performing naming and directory operations. The root of this class hierarchy is NamingException. Programs interested in dealing with a particular exception can catch the corresponding subclass of the exception. Otherwise, they should catch NamingException.
1.2.DIRECTORY PACKAGE
The javax.naming.directory package extends the javax.naming package to provide functionality for accessing directory services in addition to naming services. This package allows applications to retrieve attributes associated with objects stored in the directory and to search for objects using specified attributes.
The Directory Context The DirContext interface represents a directory context. It defines methods for examining and updating attributes associated with a directory object. You use getAttributes() to retrieve the attributes associated with a directory object (for which you supply the name). Attributes are modified using modifyAttributes(). You can add, replace, or remove attributes and/or attribute values using this operation. DirContext also behaves as a naming context by extending the Context interface. This means that any directory object can also provide a naming context. For example, a directory object for a person might contain attributes about that person as well as
Enterprise Java Jan-June 2014
Prepared by: Mr. Hitesh Kumar Sharma & Mr. Ravi Tomar Page 6 Prepared for : B.Tech CS VI Sem (MFT+O&G+OSS+CCVT)
provide a context for naming objects, such as the person's printers and file system relative to that person directory object.
Searches DirContext contains methods for performing content-based searching of the directory. In the simplest and most common form of usage, the application specifies a set of attributes--possibly with specific values--to match and submits this attribute set to the search() method. Other overloaded forms of search() support more sophisticated search filters.
Enterprise Java Jan-June 2014
Prepared by: Mr. Hitesh Kumar Sharma & Mr. Ravi Tomar Page 7 Prepared for : B.Tech CS VI Sem (MFT+O&G+OSS+CCVT)
2.0. JMS (JAVA MESSAGING SERVICES)
Messaging is a method of communication between software components or applications.
A messaging system is a peer-to-peer facility
o A messaging client can send messages to, and receive messages from, any other
client.
o Each client connects to a messaging agent that provides facilities for creating,
sending, receiving, and reading messages.
Messaging enables distributed communication that is loosely coupled.
o A component sends a message to a destination, and the recipient can retrieve the
message from the destination.
o The sender and the receiver do not have to be available at the same time in order
to communicate.
o The sender does not need to know anything about the receiver; nor does the
receiver need to know anything about the sender.
o The sender and the receiver need to know only which message format and which
destination to use.
Messaging also differs from e-mail.
o E-mail is a method of communication between people or between software
applications and people.
o Messaging is used for communication between software applications or software
components.
2.1.MESSAGE-ORIENTED MIDDLEWARE (MOM)
Message-oriented middleware is a software application or messaging agent that provides
facilities for creating, sending, receiving, and reading messages synchronously or
asynchronously between system components.
A client which produces the message is called a producer and the client which receives the
Enterprise Java Jan-June 2014
Prepared by: Mr. Hitesh Kumar Sharma & Mr. Ravi Tomar Page 8 Prepared for : B.Tech CS VI Sem (MFT+O&G+OSS+CCVT)
message is called a consumer. All the messages sent are stored in a particular location which is
called a destination.
2.2. JMS API
The Java Message Service is a Java API that allows applications to create, send, receive, and
read messages. The JMS API defines a common set of interfaces and associated semantics that
allow programs written in the Java programming language to communicate with other
messaging implementations.
The JMS API enables communication that is not only loosely coupled but also,
Asynchronous: A JMS provider can deliver messages to a client as they arrive; a client does not
have to request messages in order to receive them.
Reliable: The JMS API can ensure that a message is delivered once and only once. Lower levels of
reliability are available for applications that can afford to miss messages or to receive duplicate
messages.
Enterprise Java Jan-June 2014
Prepared by: Mr. Hitesh Kumar Sharma & Mr. Ravi Tomar Page 9 Prepared for : B.Tech CS VI Sem (MFT+O&G+OSS+CCVT)
2.3.JMS APPLICATION
A JMS application is composed of the following parts:
JMS Clients – These are the Java language programs that send and receive messages.
Non-JMS Clients – These are clients that use a message system’s native client API instead of JMS.
Messages – Each application defines a set of messages that are used to communicate information
between its clients.
JMS Provider – This is a messaging system (MOM) that implements JMS API in addition to the other
administrative and control functionality required of a full featured messaging product.
Administered Objects – Administered objects are preconfigured JMS objects created by an
administrator for the use of clients.
2.4.JMS ADMINISTERED OBJECTS
There are two types of JMS administered objects:
ConnectionFactory – This is the object a client uses to create a connection with a provider.
Destination – This is the object a client uses to specify the destination of messages it is sending and
the source of messages it receives.
Administered objects are placed in a JNDI namespace by an administrator.
Enterprise Java Jan-June 2014
Prepared by: Mr. Hitesh Kumar Sharma & Mr. Ravi Tomar Page 10 Prepared for : B.Tech CS VI Sem (MFT+O&G+OSS+CCVT)
2.5.JMS MESSAGING MODELS
The JMS API supports two models:
Point-to-Point (PTP) Model
In point-to-point messaging model, a producer creates and sends a message which will be received
only by a single consumer.
In point-to-point model, the message destination is called “Queue”.
PTP characteristics,
o Each message has only one consumer.
o A sender and a receiver of a message have no timing dependencies. The receiver can fetch
the message whether or not it was running when the client sent the message.
o The receiver acknowledges the successful processing of a message.
Enterprise Java Jan-June 2014
Prepared by: Mr. Hitesh Kumar Sharma & Mr. Ravi Tomar Page 11 Prepared for : B.Tech CS VI Sem (MFT+O&G+OSS+CCVT)
Publish/Subscribe Model
In publish/subscribe model, any number of clients can subscribe to a message destination and
when a client sends (publishes) a message to the destination, that message is received by all clients
who have subscribed to that destination.
In this model, the message producer is called publisher and the consumers are called subscribers.
Also the message destination is called “Topic”.
Publish/Subscribe model characteristics,
o Each message can have multiple consumers.
o Publishers and subscribers have a timing dependency. A client that subscribes to a topic
can consume only messages published after the client has created a subscription, and the
subscriber must continue to be active in order for it to consume messages.
Enterprise Java Jan-June 2014
Prepared by: Mr. Hitesh Kumar Sharma & Mr. Ravi Tomar Page 12 Prepared for : B.Tech CS VI Sem (MFT+O&G+OSS+CCVT)
2.6.JMS PROGRAMMING MODEL
ConnectionFactory
an administered object that encapsulates a set of connection configuration parameters.
o A client uses it to create a Connection to a JMS provider.
o JMS clients find administered objects by looking them up in a JNDI namespace.
o Connection factories come in two forms implementing either QueueConnectionFactory or
TopicConnectionFactory interfaces.
Enterprise Java Jan-June 2014
Prepared by: Mr. Hitesh Kumar Sharma & Mr. Ravi Tomar Page 13 Prepared for : B.Tech CS VI Sem (MFT+O&G+OSS+CCVT)
At the beginning of a JMS client program, you usually perform a JNDI API lookup of the connection
factory.
For example, the following code fragment obtains an InitialContext object and uses it to look up the
QueueConnectionFactory and the TopicConnectionFactory by name. You can choose any of the