Invoking EJBs / JNDI / JMS over HTTP in JBoss EAP 7.2+ with or without a Load Balancer
Environment
- Red Hat Enterprise Application Platform (EAP) 7.1 or later
- Remote Enterprise Java Beans (EJB)
- Java Messaging Service (JMS)
- HTTP protocol
Issue
- How to configured and invoke
- How to disable / enable
- Need to change the default
wildfly-servicespath - Load-balancer friendly protocol needed
Resolution
Note: When using a load balancer, it needs to be a httpd-based load balancer for EJB over http through a load balancer to work correctly.
The implementation is simple using the default configuration:
-
Add the following dependency to the client code:
<dependency> <groupId>org.wildfly.wildfly-http-client</groupId> <artifactId>wildfly-http-ejb-client</artifactId> </dependency> -
Example Client Context Code
public void callRemoteEjbOverHttp() { HelloRemote remote = getInitialContext(host, port, user, pass).lookup("ejb:helloWorld/helloWorld-ejb/HelloWorldSLSB!org.jboss.examples.ejb.HelloRemote"); remote.helloWorld(); } public static Context getInitialContext(String host, Integer port, String username, String password) throws NamingException { Properties props = new Properties(); props.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory"); // https://localhost:8080/wildfly-services props.put(Context.PROVIDER_URL, String.format("%s://%s:%d/wildfly-services", "http", host, port)); if(username != null && password != null) { props.put(Context.SECURITY_PRINCIPAL, username); props.put(Context.SECURITY_CREDENTIALS, password); } return new InitialContext(props); }
These are few useful configuration commands.
Changing the default wildfly-services path
The default path wildfly-services can be changed in the undertow subsystem, for example the CLI command below changes it to be http, where the client would use the JNDI provider URL of http://localhost:8080/http
/subsystem=undertow/server=default-server/host=default-host/setting=http-invoker:write-attribute(name=path,value=http)
Disabling EJB / JNDI over HTTP
Remove the http-invoker from the host in the undertow subsytem for example:
/subsystem=undertow/server=default-server/host=default-host/setting=http-invoker:remove()
Re-enable EJB / JNDI over HTTP
This CLI command below will add the default http-invoker configuration back to the default-host in the undertow subsystem:
/subsystem=undertow/server=default-server/host=default-host/setting=http-invoker:add(security-realm=ApplicationRealm, path=/wildfly-services)
Related Solutions
Note EJB / JNDI over HTTP is support for JBoss EAP 7.2 but Tech Preview for 7.1
Root Cause
Server Side
The server-side, enabled by default in JBoss EAP 7.2, consists of a service that handles the incoming HTTP requests, unmarshals them and passes the result to the internal EJB invocation code.
It can also handle accepting and pushing JMS messages.
NOTE: Any deployment that aims to use the http-authentication-factory must use Elytron security with the same security domain corresponding to the specified HTTP authentication factory.
Client Side
The client-side implementation consists of an EJBReceiver that uses the Undertow HTTP client to invoke the server. Connection management is handled automatically using a connection pool.
To perform the HTTP invocation, you must use the http or https URL and include the context name of the HTTP invoker, which defaults to wildfly-services. For example, if remoting-based code is currently using remote+http://localhost:8080 as the target URL, in order to implement HTTP transport, this would correspond to http://localhost:8080/wildfly-services.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.