Creating message-driven beans

You can use the Create an Enterprise Bean wizard to create EJB 2.x message-driven beans.

About this task

Message-driven beans were introduced in EJB 2.0 to support the processing of asynchronous messages from a Java™ Message Service (JMS). The EJB 2.1 specification expands the definition of the message-driven bean so that it can support any messaging system, not just JMS. Accordingly, the deployment descriptor elements used to define message-driven beans changed in the EJB 2.1 specification. The wizard for creating an EJB 2.1 message-driven bean also includes additional pages for optionally defining your non-JMS messaging system and its activation configuration elements.

See Message-driven bean deployment descriptor examples for examples of deployment descriptor entries for message-driven beans in the EJB 2.0 and EJB 2.1 specifications.

Procedure

  1. In the Java EE perspective, click File > New > Other. Select EJB > Enterprise Bean (1.x-2.x) and click Next. Alternatively, right click your EJB project and click New > Enterprise Bean (1.x-2.x).
  2. Select Message-driven bean.
  3. Select the EJB Project that you want to add the bean to. You can choose EJB 2.0 or EJB 2.1 projects.
  4. In the Bean name field, type the name that you want to assign to the enterprise bean. By convention, bean names begin with an uppercase letter.
    Note: You can use Unicode characters for the bean name, but Unicode characters are not supported for enterprise bean packages and classes associated with enterprise beans.
  5. In the Source folder field, select the source folder for the new bean.
  6. In the Default package field, enter the package name for the new bean.
  7. Optional: You can select Generated an annotated bean class. If you select this option, the wizard generates annotations at the beginning of the Java code for the bean class. The annotations define the implementation of the bean as you specify in the wizard. The annotations are then used by the EJB tools to generate the necessary bean classes, and to provide values that are inserted in the EJB deployment descriptor (ejb-jar.xml). Select this option if you are familiar with annotations and want to use the annotations to update the bean rather than using the deployment descriptor. Other options might not be available if you select this option.

    For more information, see Annotation-based programming overview.

  8. Click Next.
  9. For EJB 2.1 message-driven beans (skip to Step 10 if you are creating an EJB 2.0 bean), specify the following:
    1. Specify the messaging service to use for the message-driven bean.
      • JMS type: Select this option if you want to use a JMS messaging service, using the javax.jms.MessageListener class as the listener type.
      • Other type: Select this option if you want to use a non-JMS or custom messaging service. In the Listener type field, enter the fully qualified name of the listener class.
      Click Next.
    2. Specify the details for the message-driven bean:
      Note: If you are creating an EJB 2.1 message-driven bean that uses a non-JMS messaging system, some options are not available but can be defined manually as activation configuration name-value pairs.
      • Transaction type: You can specify that either the container or the bean manages the transactions of the bean.
      • Activation Configuration: Use this table to configure additional properties. Each name-value pair that you define is added to the deployment descriptor as an <activation-config-property> element. For EJB 2.1 message-driven beans that use JMS messaging, the predefined properties include the following:
        acknowledgeMode
        Values include Auto-acknowledge and Dups-ok-acknowledge.
        destinationType
        Values include javax.jms.Queue and javax.jms.Topic.
        messageSelector
        Values are user-specified.
        Click Add to define additional properties in the table or click Remove to delete a property from the table.
      • If you want the new bean to inherit from an existing bean in the same EJB project, select a Bean supertype. The Bean supertype drop-down box is enabled if other CMP beans exist in the EJB project. Selecting a Bean supertype disables the Key class field since subtypes must have the same key class as the supertype.
      • Bean class: Enter the package and class name for the bean class. By default, the wizard suggests a bean class based on the bean and package names that you defined. A bean class can be a new class that the wizard generates, or it can be an existing class in the project class path. Click the Class button to open the Type Selection window that lists the classes in the project that correspond to the bean type. The name of the bean class appears blue for existing classes with source. The name of the bean class appears red for existing binary classes.
      • Message Destination Link: Click Browse to link to a message destination. This adds a message-destination-link element to the bean in the deployment descriptor. The message destination link is not available if you selected to generate an annotated bean class.
    3. Click Next.
    4. Optional: In the Bean superclass field, type or select the class that you want. This field is disabled if a bean supertype was specified. Click Next.
    5. Optional: To Add bean to Class Diagram, select this check box and select a class diagram from the explorer view of your projects. There is a default Class Diagram already created in each EJB project. If you want to create a new Class Diagram, click New to open the New Class Diagram wizard.
    6. Click Finish. The new bean is generated and defined in the deployment descriptor.
  10. For EJB 2.0 message-driven beans, specify the following:
    1. Specify the details for the message-driven bean. Note: JMS-type messaging is implied for all 2.0 message-driven beans.
      • Transaction type: You can specify that either the container or the bean manages the bean's transactions.
      • Acknowledge mode: For bean-managed transactions, you can specify how the session acknowledges any messages it receives. Choices are Auto-acknowledge and Dups-ok-acknowledge.
      • Message-driven destination: Select a Destination type of Queue or Topic:
        • Queue specifies that the point-to-point JMS messaging model is used.
        • Topic specifies that the publish-and-subscribe JMS messaging model is used.
      • Durability: If you selected a message destination type of Topic, you can specify whether a topic subscription is durable or non-durable:
        • Durable: A subscriber registers a durable subscription with a unique identity that is retained by JMS. Subsequent subscriber objects with the same identity resume the subscription in the state it was left in by the earlier subscriber. If there is no active subscriber for a durable subscription, JMS retains the messages of the subscription until they are received by the subscription or until they expire.
        • NonDurable: The subscriptions last for the lifetime of their subscriber object. This means that a client sees the messages published on a topic only while its subscriber is active. If the subscriber is not active, the client is missing messages published on its topic.
      • Bean class: Enter the package and class name for the bean class. By default, the wizard suggests a bean class based on the bean name and default package that you defined. A bean class can be a new class that the wizard generates, or it can be an existing class in the project class path. Click the Class button to open the Type Selection window that lists the classes in the project that lists the classes in the project that correspond to the bean type. The name of the bean class appears blue for existing classes with source. The name of the bean class appears red for existing binary classes.
      • Message selector: The JMS message selector is used to determine which messages the message bean receives.
    2. Click Next.
    3. Optional: In the Bean superclass field, type or select the class that you want. This field is disabled if a bean supertype was specified. Click Next.
    4. Optional: To Add bean to Class Diagram, select this check box and select a class diagram from the explorer view of your projects. There is a default Class Diagram already created in each EJB project. If you want to create a new class diagram, click New to open the New Class Diagram wizard.
    5. Click Finish. The new bean is generated and defined in the deployment descriptor.

What to do next

Tip: You can use the Bean page of the EJB deployment descriptor editor to review the bean and change or add any additional settings or extensions and bindings.

Feedback