EJB 3 In Action - Deployment descriptor reference

239 阅读5分钟

ejb-jar.xml

ejb-jar.xml is the optional deployment descriptor that’s packaged in an EJB module. ejb-jar.xml has two primary elements:

  • enterprise-beans is used to define beans, resources, and services used by the beans
  • assembly-descriptor is used to declare security roles, method permissions, declarative transaction settings, and interceptors.
<ejb-jar
  xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/ejb-jar_3_2.xsd"
  version="3.2">

<module-name>

<module-name> is used to define the name of the EJB module when deployed to the Enterprise server:

<ejb-jar...>
    <module-name>action-bazaar-ejb</module-name>
</ejb-jar>

It’s only applicable to standalone EJB-JARS or EJB-JARS packaged in an EAR. It’s ignored if used within a .war file, in which case standard .war file module name rules apply.

Using <module-name> can standardize the name of your module when deployed, no matter the version number.

<enterprise-beans>

The <enterprise-beans> tag is used to define EJBs in an EJB-JAR module. You can use <session> or <message-driven> subtags to define session or message-driven beans.

<ejb-jar...>
    <enterprise-beans>
        <session>...</session>
    </enterprise-beans>
</ejb-jar>
Tags to configure <session>
Tag nameDescription
ejb-nameA logical name for the session bean. Property "name" of @Stateless or @Stateful annotation.
mapped-nameA vendor-specific name for the bean. Property "mapped-Name" of the @Stateless or @Stateful annotation.
remoteRemote interface for the EJB: @Remote.
localLocal interface of the EJB: @Local.
service-endpointWeb service endpoint interface for the EJB. Applies only to stateless beans: @WebService.
ejb-classFully qualified name of the bean class.
session-typeType of session bean: @Stateless @Stateful @Singleton.
stateful-timeoutThe amount of time a stateful bean can be idle before eligible for cleanup by the container: @StatefulTimeout.
timeout-methodThe callback method for programmatically created timers: @Timeout.
timerAn EJB timer. Created by the container on deployment. Callbacks made to the timeout-method.
init-on-startupSingleton bean should be created at deployment: @Startup.
concurrency-management-typeSpecifies concurrency for singleton and stateful beans. May be either Bean or Container: @Concurrency-Management.
concurrent-methodConfigures the container managed concurrency for a method.
depends-onA list of one of more singleton beans that must be initialized before this singleton bean. Applies only to singleton beans. Use the ejb-link syntax to specify the dependencies: @DependsOn.
init-methodEJB 2-style create method for EJB 3 stateful EJBs: @Init.
remove-methodEJB 2-style remove method for EJB 3 stateful beans: @Remove.
async-methodSpecifies method for asynchronous execution: @Asynchronous.
transaction-typeSpecifies transaction for an EJB. May either Bean or Container: @TransactionManagement.
after-begin-methodTransaction callback method for a stateful bean to inform it that a new transaction has started: @AfterBegin.
before-completion-methodTransaction callback method for a stateful bean to inform it that a transaction is about to be committed: @BeforeCompletion.
after-completion-methodTransaction callback method for a stateful bean to inform it that a transaction has finished committing. Both commit and rollback will call this method: @AfterCompletion.
around-invokeThe method name on a class to be called during the around-invoke portion of an EJB invocation: @AroundInvoke.
around-timeoutThe method name on a class to be called during the around-timeout portion of an EJB invocation: @AroundTimeout.
post-activateLifecycle callback method after activation: @PostActivate.
pre-passivateLifecycle callback method before passivation: @PrePassivate.
security-role-refReferences internal roles to external roles.
security-identityUse the caller’s security identity to enforce security or override the caller’s identity for the specified “run-as” identity.
passivation-capableIs the stateful bean able to be passivated?
<ejb-jar...>
    <enterprise-beans>
        <message-driven>...</message-driven>
    </enterprise-beans>
</ejb-jar>
Tags to configure <message-driven>
Tag nameDescription
ejb-nameThe logical name for the MDB. Property "name" of @MessageDriven.
mapped-nameA vendor-specific name for the bean. Property "mappedName" of @MessageDriven.
ejb-classFully qualified name of the bean class.
messaging-typeMessaging type supported—that is, the message listener interface of the MDB. Property "messageListenerInterface" of @MessageDriven.
timeout-methodThe callback method for programmatically created timers: @Timeout.
timerAn EJB timer. Created by the container on deployment. Callbacks made to the timeout-method.
transaction-typeSpecifies transaction for an EJB. May be either Bean or Container. @TransactionManagement
message-destination-typeExpected type of the destination—that is, javax.jms.Queue.
message-destination-linkA vendor-specific mapping of a logical destination with the actual destination.
activation-configName/value pairs to configure the MDB when running in the EJB container. Property "activationConfig" of @MessageDriven.
around-invokeThe method name on a class to be called during the around-invoke portion of an EJB invocation: @AroundInvoke.
around-timeoutThe method name on a class to be called during the around-timeout portion of an EJB invocation: @AroundTimeout.
security-role-refReferences internal roles to external roles.
security-identityUse the caller’s security identity to enforce security or override the caller’s identity for the specified “run-as” identity.

Interceptors

<ejb-jar...>
    <interceptors>
        <interceptor>...</interceptor>
    </interceptors>
</ejb-jar>
Tags to configure <interceptor>

he <interceptors> tag is used to define interceptors for your EJB module

Tag nameDescription
descriptionDescription of this interceptor.
interceptor-classThe fully qualified name of the class to serve as your interceptor.
around-invokeThe method name on a class to be called during the around-invoke portion of an EJB invocation: @AroundInvoke.
around-timeoutThe method name on a class to be called during the around-timeout portion of an EJB invocation: @AroundTimeout.
around-constructThe method name on a class to be called during an object’s construction: @AroundConstruct.
post-activateLifecycle callback method after activation: @PostActivate.
pre-passivateLifecycle callback method before passivation: @PrePassivate.

<assembly-descriptor>

The <assembly-descriptor> tag is used to define declarative transactions, security role and method permissions, and interceptor bindings:

<ejb-jar...>
    <assembly-descriptor>...</assembly-descriptor>
</ejb-jar>

<security-role>

The <security-role> tag is used to define security roles used in the application:

<ejb-jar...>
    <assembly-descriptor>
        <security-role>...</security-role>
    </assembly-descriptor>
</ejb-jar>

The corresponding annotation is @DeclareRoles.

Tag to configure <security-role>
Tag nameDescription
role-nameThe name of the security role. Example: @DeclareRoles({"Admin", "User"}).

<method-permission>

The <method-permission> tag is used to define what roles are allowed to execute which EJB methods:

<ejb-jar...>
    <assembly-descriptor>
        <method-permission>...</method-permission>
    </assembly-descriptor>
</ejb-jar>

The corresponding annotation is @RolesAllowed

Tags to configure <method-permission>
Tag nameDescription
role-nameName of the roll allowed to execute the method. This or unchecked may be used but not both.
uncheckedSpecifies that all roles are allowed to execute the method. This or role-name may be used but not both.
methodSpecifies the name of the EJB (as specified in ) and the name of the method to secure.

<container-transaction>

<ejb-jar...>
    <assembly-descriptor>
        <container-transaction>...</container-transaction>
    </assembly-descriptor>
</ejb-jar>

The corresponding annotation is @TransactionAttribute

Tags to configure <container-transaction>
Tag nameDescription
trans-attributeSpecifies the transaction attribute of the method. Valid values are Required, RequiresNew, NotSupported, Supports, Never, Mandatory.
methodSpecifies the name of the EJB (as specified in <session> <ebj-name>) and the name of the method to secure.

<interceptor-binding>

    
The `<interceptor-binding>` tag is used to bind interceptors to EJBs at either a class level or a method level:
    
<ejb-jar...>
    <assembly-descriptor>
        <interceptor-binding>...</interceptor-binding>
    </assembly-descriptor>
</ejb-jar>

This tag is similar to the @Interceptors annotation.

Tags to configure <interceptor-binding>
Tag nameDescription
ejb-nameSpecifies the name of the EJB, as specified in <session><ebj-name> or the "name" element of @Stateless or @Stateful. This will apply the interceptor to all methods of the class. A value of “*” will create a default interceptor and bind to all EJB and MDB in the ejb-jar.xml file or .war file.
<interceptor-order> <interceptor-class>..</> </interceptor-order>A list of interceptor classes that are bound to the contents of the ejb-name element. The interceptor-order is optional, and if used specifies the order in which the interceptors are to be called. No corresponding annotation for specifying order.
exclude-default-interceptorsSpecifies that default interceptors aren’t to be applied to the EJB class or method: @ExcludeDefaultInterceptors.
exclude-class-interceptorsSpecifies that class interceptors aren’t to be applied to the EJB class or method: @ExcludeClassInterceptors.
methodSpecifies which method.

<message-destination>

<ejb-jar...>
    <assembly-descriptor>
        <message-destination>...</message-destination>
    </assembly-descriptor>
</ejb-jar>

The <message-destination> tag specifies a logical destination name that’s later mapped to a physical destination by the deployer:

 Tags to configure <message-destination>
Tag nameDescription
message-destination-nameSpecifies a name for a message destination. This name must be unique among the names of message destinations within the ejb-jar.xml or .war file.
mapped-nameAn application-specific, nonportable (not required to be supported) name that this destination should be mapped to.
lookup-nameThe JNDI name to be looked up to resolve the message destination. This is typically the location in JNDI where your application server has put the destination.

<exclude-list>

The <exclude-list> tag specifies a list of methods that are denied permission to execute:

<ejb-jar...>
    <assembly-descriptor>
        <exclude-list>...</exclude-list>
    </assembly-descriptor>
</ejb-jar>

This tag is equivalent to the @DenyAll annotation

Tags to configure <exclude-list>
Tag NameDescription
methodSpecifies the name of the EJB (as specified in <session><ebj-name>) and the name of the method to secure.

<application-exception>

The <application-exception> tag specifies a list of application-specific exceptions that may be thrown by your EJBs and MDBs:

<ejb-jar...>
    <assembly-descriptor>
        <application-exception>...</application-exception>
    </assembly-descriptor>
</ejb-jar>

By listing application exceptions, you can configure whether the exception will cause a rollback of the current transaction. By default, an EJBException will trigger a rollback but application exceptions do not. This tag corresponds to the @ApplicationException annotation.

Tags to configure <application-exception>
Tag nameDescription
exception-classSpecifies the fully qualified name of the exception class.
rollbackSpecifies if the container should roll back the transaction before throwing the exception up to the client.