# EJB

## EJB Component

Available as of Apache Camel 2.4

The ejb: component binds EJBs to message exchanges.

## URI format

    ejb:ejbName[?options]

Where ejbName can be any string which is used to look up the EJB in the Application Server JNDI Registry

## Options

Name | Type | Default | Description
--- | --- | --- | ---
`method` | `String` | `null` | The name of the method to use to invoke the bean. If not supplied, Apache Camel tries to pick the method itself. In case of ambiguity, an exception is thrown. See Bean Binding for more details.
`multiParameterArray` | `boolean` | `false` | Determines how to treat the parameters passed from the message body; if `true`, the In message body should be an array of parameters.

You can append query options to the URI in the following format, `?option=value&option=value&...`

The EJB component extends the Bean component, by which most of the details from the Bean component also apply to this component.

## Bean Binding

How bean methods to be invoked are chosen (when not specified explicitly through the method parameter) and how parameter values are constructed from the Message are all defined by the Bean Binding mechanism, which is used throughout all of the various Bean Integration mechanisms in Apache Camel.

## Examples

In the following examples we use the Greater EJB which is defined as follows:

    public interface GreaterLocal {

        String hello(String name);

        String bye(String name);

    }

And the implementation

    @Stateless
    public class GreaterImpl implements GreaterLocal {

        public String hello(String name) {
            return "Hello " + name;
        }

        public String bye(String name) {
            return "Bye " + name;
        }

    }

## Using Java DSL

In this example we want to invoke the `hello` method on the EJB. Since this example is based on an unit test using Apache OpenEJB we have to set a `JndiContext` on the EJB component with the OpenEJB settings.

    @Override
    protected CamelContext createCamelContext() throws Exception {
        CamelContext answer = new DefaultCamelContext();

        // enlist EJB component using the JndiContext
        EjbComponent ejb = answer.getComponent("ejb", EjbComponent.class);
        ejb.setContext(createEjbContext());

        return answer;
    }

    private static Context createEjbContext() throws NamingException {
        // here we need to define our context factory to use OpenEJB for our testing
        Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");

        return new InitialContext(properties);
    }

Then we are ready to use the EJB in the Apache Camel route:

    from("direct:start")
        // invoke the greeter EJB using the local interface and invoke the hello method
        .to("ejb:GreaterImplLocal?method=hello")
        .to("mock:result");

| Note
On a real application server, you'll likely not need to setup a `JndiContext` on the EJB component, as the EJB component will create a default `JndiContext` on the same JVM as the application server. This scheme typically allows the EJB component to access the JNDI registry and lookup the EJBs. However, if you need to access an application server on a remote JVM, you need to prepare the properties beforehand.

## Using XML

And this is the same example using XML instead:

Again since this is based on an unit test we need to setup the EJB component:

    <!-- setup Camel EJB component -->
    <bean id="ejb" class="org.apache.camel.component.ejb.EjbComponent">
        <property name="properties" ref="jndiProperties"/>
    </bean>

    <!-- use OpenEJB context factory -->
    <p:properties id="jndiProperties">
        <prop key="java.naming.factory.initial">org.apache.openejb.client.LocalInitialContextFactory</prop>
    </p:properties>

Before we are ready to use EJB in the Apache Camel routes:

    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="direct:start"/>
            <to uri="ejb:GreaterImplLocal?method=hello"/>
            <to uri="mock:result"/>
        </route>
    </camelContext>

- Bean

- Bean Binding

- Bean Integration