220 likes | 233 Views
Learn about the EJB Timer Service and Message Linking in EJB 2.1, including operations performed on Timer objects, steps for message linking, best practices for improving EJB transaction performance, and tips for setting optimal pool size in Message-Driven Beans.
E N D
Knowledge Byte • In this section, you will learn about: • The EJB timer service • Message linking in EJB 2.1 Collaborate
The EJB Timer Service • The EJB Timer Service: • Was introduced in EJB 2.1 specification. • Enables you to inform an enterprise bean when a specified time interval has passed. • The javax.ejb package contains the following interfaces to use the various timer services: • TimedObject: Declares the ejbTimeout() method that notifies an enterprise bean when a scheduled timer event related to the an enterprise bean occurs. • Timer: Contains the various methods that enable you to retrieve information about a timer. • TimerHandle: Declares the getTimer() method that enables you to retrieve a reference to the timer attached to the current handle. • TimerService: Declares various methods that provide access to the EJB container’s timer services. Collaborate
The EJB Timer Service (Contd.) • Operations performed on a Timer object instance during its life cycle are: • Creating: A Timer object is created by using the createTimer() method of the TimerService interface. • Saving: A Timer object is saved when theEJB container either shuts down or crashes. • Restoring: A Timer object is reactivated when either the EJB container restarts or when the getTimer() method is invoked. • Destroying: A Timer object is cancelled when the specified time interval of the Timer object ends. Timer objects can also be cancelled when either EJB container cancels the timer or the Timer object itself calls the cancel() method. Collaborate
Message Linking in EJB 2.1 • Message linking: • Is a new feature introduced in EJB 2.1 specification. • Allows the forwarding of messages sent by an enterprise bean to a message-driven bean. • Enables you to define the flow of messages between various enterprise beans and message-driven beans of an EJB application. • Between an enterprise bean and a message-driven bean is performed using the <message-destination-link> deployment descriptor tag. Collaborate
Message Linking in EJB 2.1 (Contd.) • The steps to perform the message linking in EJB are: • Specify the JMS destination name in the deployment descriptor of the enterprise bean that will send the messages. The destination name is specified in the <message-destination-link> tag of the deployment descriptor. • Specify the destination name in the <assembly-descriptor>. Destination name is specified inside the <message-destination-name> in the <assemble-descriptor> of the message sending enterprise bean. • Specify the destination name in the <message-driven-link> tag of the deployment descriptor of message-driven bean. Collaborate
From the Expert’s Desk • In this section, you will learn: • Best Practice on: • Improving EJB Transaction Performance by Setting Optimal Transaction Time Limit • Tips and Tricks on: • Specifying Optimum Size of Pool in Message-Driven Beans • JMS Tuning Techniques • Isolation Levels in Transactions • FAQs on message-driven beans and and EJB security Collaborate
Best Practices • Improving EJB Transaction Performance by Setting Optimal Transaction Time Limit • You can improve a transaction’s performance in EJB by setting an optimum time limit for the transaction to complete. This technique improves the speed and performance of an EJB application by closing the transactions that exceed the specified time limit. • You specify the time limit of a transaction in the timeout-in-seconds property of the domain.xml file. • The default time out value of a transaction is 0, which specifies that the transaction will never time out. • To achieve optimum transaction performance, you need to set the appropriate time out value according to the maximum expected time of the completion of your transaction. Collaborate
Tips and Tricks • Specifying Optimum Size of Pool in Message-driven Beans • The processes of creating and destroying a message-driven bean are resource-intensive and slow. As a result, EJB container maintains a pool of bean instances and uses one of these instances when a message is received. • You can specify the number of instances that should exist in a message-driven bean pool. EJB container maintains this number by adding and deleting instances according to the number of incoming messages. • You should specify the optimum pool size so that the incoming messages do not have to wait in the middle layer. The pool size should not be too large because it uses server resources and slows it down. • You can ensure optimum server performance by specifying the pool size as equal to the maximum expected number of concurrently arriving messages. Collaborate
Tips and Tricks • JMS Tuning Techniques • You can optimize the performance of JMS messaging by using the following techniques: • Optimize the connection between the JMS producer and consumer • Optimize the session • Select an appropriate type of destination • Optimize the JMS producer and consumer • Optimize the message • Select the appropriate JMS server Collaborate
Tips and Tricks • JMS Tuning Techniques (Contd.) • Optimizing the Connection • A Connection object represents an active connection between the JMS producer and JMS consumer. A Connection object implements the Connection interface and is created using the ConnectionFactory object. • You can use the following techniques to optimize the performance of a JMS connection: • Start the connection when required. • Execute several messages concurrently. • Release resources when finished. Collaborate
Tips and Tricks • JMS Tuning Techniques (Contd.) • Optimizing the Session • The JMS producers, consumers, and messages, are created in a Session object. Sessions implement the Session interface. • Optimization techniques while creating and using Session objects: • Choose appropriate acknowledgement mode: The Session objects have three types of acknowledgement modes: • AUTO_ACKNOWLEDGE: In this mode, the server automatically acknowledges the messages when they are received. • CLIENT_ACKNOWLEDGE: In this mode, the sender waits for the acknowledgement before sending next message. • DUPS_OK_ACKNOWLEDGE: In this mode, the sender sends a message several times to the server in order to ensure the delivery of the message. Collaborate
Tips and Tricks • JMS Tuning Techniques (Contd.) • Create separate Session objects for transactional and non transactional message types. • Close the session when finished: You need to close a session when messaging is complete, to free the server and network resources associated with the session. • Selecting Appropriate Type of Destination • To optimize the performance of messaging, you can configure various features of the destination, such as maximum storage space, maximum number of messages, and priority of messages. Collaborate
Tips and Tricks • JMS Tuning Techniques (Contd.) • Optimizing the JMS Producer and Consumer • Select the appropriate delivery mode: You can select the delivery mode of the messages as durable or non durable. • Select asynchronous messages: JMS supports messaging in two modes, synchronous and asynchronous. To improve the performance of JMS messaging, you need to use asynchronous messaging whenever possible. Collaborate
Tips and Tricks • JMS Tuning Techniques (Contd.) • Optimizing the Message • A message contains the information that you need to communicate between two Java components and is implemented using the Message object. • JMS defines five types of Message objects: Text, Object, Byte, Stream, and Map. Each message type has certain characteristics that make it suitable for a particular type of communication. • You need to select the suitable message type that fulfills the communication requirements of your application. Collaborate
Tips and Tricks • JMS Tuning Techniques (Contd.) • Selecting the JMS server type • There are various types of JMS servers. You need to select the JMS server that best suits your messaging requirements. • Features to determine the suitability of the JMS server for your messaging needs • Whether the server supports the clustering or not. • Maximum number of open connections required concurrently. • Frequency of incoming messages. • Type and size of incoming messages. • Whether to use the durable or non-durable delivery mode. Collaborate
Tips and Tricks • JMS Tuning Techniques (Contd.) • Isolation Levels in Transactions • Transactions isolate the data that is being modified by the statements inside it. The data is isolated so that other transactions do not read the wrong values that might result from an unsuccessful transaction. • EJB specification enables you to define the isolation levels that control the degree to which the data being used in a transaction is visible to other transactions. • You need to choose the appropriate isolation level based on the data consistency and the performance requirements of your application. Collaborate
Tips and Tricks • JMS Tuning Techniques (Contd.) • EJB defines four transaction isolation levels: • READ UNCOMMITTED: This level imposes no restrictions on concurrent transactions. • READ COMMITTED: This isolation level allows reading of data that is written by a committed transaction. In this mode, a transaction cannot read the data that is still being used by another transaction. • REPEATABLE READ: This isolation level ensures that the data does not change between two consecutive reads from the same transaction. • SERIALIZABLE READ: In this mode, the transactions run in a serialized form, that is one after another. The processes also do not share any data with one another. Collaborate
FAQs • Why are the message-driven beans used to transfer messages between the components of a J2EE application? • Remote Method Invocation (RMI) has the disadvantage that it can only support one message sender and one message receiver and the delivery of message to the receiver is not guaranteed. The message-driven beans solve this problem by using JMS API, which supports asynchronous messaging between multiple senders and multiple receivers. • Using message-driven beans, messaging process is faster as compared to RMI because the message-driven beans use fewer server resources and the senders do not wait for the acknowledgement from the receiver. • What are the two interfaces implemented by a message-driven bean class? • The message-driven bean class implements two interfaces, javax.ejb.MessageDrivenBean and javax.jms.MessageListener. Collaborate
FAQs (Contd.) • What are the transaction attributes of container-managed transactions? • Transaction attributes are used to declare how the transactions are handled in CMP enterprise beans. Various transaction attributes are: Required, RequiresNew, Supports, Mandatory, NotSupported, and Never • What are the two types of security techniques in EJB? • EJB security consists of two techniques, authentication and authorization. Authentication involves checking the identity of the client that logs on to the application server to access an application. Authorization involves checking that the clients perform only those operations that they are allowed to perform. Collaborate
FAQs (Contd.) • Which API is used to implement security in EJB? • Security in EJB is implemented using the java.security interface. • How can you declare method permissions for EJB-tier resources? • The method permission for EJB-tier resource is declared using the <method-permission> tag in the deployment descriptor. Collaborate
Challenge • Message-driven beans are configured to select messages using the ___________ deployment descriptor tag. • The transaction attribute to ensure that a new transaction is started is ____________. • To participate in a transaction, a session bean must implement the ____________ interface. • The __________ method verifies the security role of an EJB client. • The ______________ exception is thrown if an enterprise bean fails in the authentication and authorization process. • Which tag of the deployment descriptor is used to set the transaction type of EJB transactions? • Which enterprise beans can manage their own transactions? Collaborate
Solutions to Challenge • <message-selector> • RequiresNew • javax.ejb.SessionSynchronization interface • isCallerInRole(String role) • java.lang.SecurityException • <transaction-type> • Bean managed transaction Collaborate