Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Coding EJB clients for JBoss EAP 7.1

June 14, 2017
Francesco Marchioni
Related topics:
Java
Related products:
Red Hat JBoss Enterprise Application Platform

    This article summarizes some new features that will be enabled in EAP 7.1 for applications using remote EJB clients. These new features will improve some aspects of the remote EJB communication such as:

    • A simplified method for looking up remote EJBs
    • A new annotation to control transaction propagation from remote EJB clients
    • A new annotation to enable Client side interceptors for EJB calls
    • An update in the remote EJB client configuration file
    • Simplified lookup of remote EJBs

    Let's see them in detail:

    Simplified Initial context lookup

    Traditionally, the lookup of remote EJBs required a bit of complexity for the developers (See the following solution). The most common approach was to embed the remote destination host and port (along with security information) into a separate configuration file (jboss-ejb-client.properties). Although this decoupling is considered a good design practice, for the sake of writing quickly a simple application client with as little as a Java class, you can now embed this information into the Initial Context, through the "remote+http" protocol as in the following example:

    import org.jboss.ejb.client.EJBClient;
    
    public void callRemoteEjb() {
       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) {
       Properties props = new Properties();
       props.put(Context.INITIAL_CONTEXT_FACTORY,  "org.wildfly.naming.client.WildFlyInitialContextFactory");
       props.put(Context.PROVIDER_URL, String.format("%s://%s:%d", "remote+http", host, port));
       props.put(Context.SECURITY_PRINCIPAL, username)
       props.put(Context.SECURITY_CREDENTIALS, password);
       return new InitialContext(props);
    }

    Also, please note that, since EAP 7.1, the new Initial Context Factory to be used for the lookup is now org.wildfly.naming.client.WildFlyInitialContextFactory.

    New remote configuration file

    The file jboss-ejb-client.properties which we have already mentioned, can still be used is used to define the remote connection properties and authentication details, however, it's now deprecated. EAP 7.1 features the new Elytron security framework, so you are encouraged to migrate to the wildfly-config.xml file, which allows a wider range of authentication/authorization options. As a proof of concept, the following wildfly-config.xml can be used to allow an SASL Digest-MD5 authentication using the credentials "admin" and "password123!".

    <configuration>
       <authentication-client xmlns="urn:elytron:1.0">
          <authentication-rules>
             <rule use-configuration="default" />
          </authentication-rules>
          <authentication-configurations>
             <configuration name="default">
                <allow-sasl-mechanisms names="DIGEST-MD5" />
                <forbid-sasl-mechanisms names="JBOSS-LOCAL-USER" />
                <set-user-name name="admin" />
                <credentials>
                   <clear-password password="password123!" />
                </credentials>
                <use-service-loader-providers />
             </configuration>
          </authentication-configurations>
       </authentication-client>
       <jboss-ejb-client xmlns="urn:jboss:wildfly-client-ejb:3.0">
          <connections>
             <connection uri="${remote.ejb.url}" />
          </connections>
       </jboss-ejb-client>
    </configuration>

    Also, please notice in the above example, that the EJB Client relevant part (in the jboss-ejb-client stanza) is where we provide the Connection URI as a System Property.

    EJB Client interceptors

    EJB Interceptors are mostly used, as the word suggest, to intercept a call to an EJB method so that you can apply any proxy logic to it. For example, you could log the call to the EJB or deny accessing some methods under some conditions. EJB Interceptors can be however placed also in the Client side of the remote call. Until JBoss EAP 7.1, if you wanted to register Interceptors to the remote EJB client communication you had to apply one of the following approaches:

    • Programmatically:
    EJBClientContext.getCurrent().registerInterceptor(0, new HelloClientInterceptor());
    • Creating a file in the META-INF/services directory named org.jboss.ejb.client.EJBClientInterceptor and list the fully qualified name of your client interceptor class. For example:
    $ echo "com.jboss.examples.ejb3.client.HelloClientInterceptor" > META-INF/services/org.jboss.ejb.client.EJBClientInterceptor

    Now since EAP 7.1 there is now annotation @org.jboss.ejb.client.annnotation.ClientInterceptors which makes the client side work very similar to the way the standard EE service side interceptors work:

    import org.jboss.ejb.client.annotation.ClientInterceptors;
    @ClientInterceptors({HelloClientInterceptor.class})
    
    public interface HelloBeanRemote {
       public String hello();
    }

    Here is the sample Client Interceptor which implements the EJBClientInterceptor interface:

    import org.jboss.ejb.client.EJBClientInterceptor;
    import org.jboss.ejb.client.EJBClientInvocationContext;
    
    public class HelloClientInterceptor implements EJBClientInterceptor {
    
       @Override
       public void handleInvocation(EJBClientInvocationContext context) throws Exception {
    
          context.getContextData().put(CLIENT_DATA, clientData);
     
          context.sendRequest();
       }
    
       @Override
       public Object handleInvocationResult(EJBClientInvocationContext context) throws Exception {
    
       try {
          return context.getResult();
       } finally {
    
            context.getContextData().get(CLIENT_DATA), SERVER_DATA, context.getContextData().get(SERVER_DATA)));
         }
       }
    }

    The new ClientTransaction annotation

    In EAP 7.1 a new annotation, named @org.jboss.ejb.client.annotation.ClientTransaction has been introduced to handle transaction propagation from an EJB Client so that you can either mandate it (i.e. fail if the client has no transaction) or prevent it (i.e. propagate no transaction even if the client has one active). You can control the policy of the ClientTransaction annotation through the Constants of org.jboss.ejb.client.annotation.ClientTransactionPolicy interface, which is the following:

    • MANDATORY: Fail with exception when there is no client-side transaction context; propagate the client-side transaction context when it is present.
    • NEVER: Invoke without propagating any transaction context; if a client-side transaction context is present, an exception is thrown.
    • NOT_SUPPORTED: Invoke without propagating any transaction context whether or not a client-side transaction context is present.
    • SUPPORTS: Invoke without a transaction if there is no client-side transaction context; propagate the client-side transaction context if it is present.

    If no annotation is present, the default policy is "org.jboss.ejb.client.annotation.ClientTransactionPolicy#SUPPORTS", which means that the transaction will be propagated if it is present, but will not fail regardless of whether or not a transaction is present.

    The annotation applies to each method of the remote interface, or to the completely remote interface as a default value. It affects only calls to that method on that interface.

    @ClientTransaction(ClientTransactionPolicy.MANDATORY)
    public interface RemoteCalculator  {
       public void callRemoteEjb() { }
    }
    
    @Stateless
    @Remote(RemoteCalculator.class)
    public class CalculatorBean implements RemoteCalculator {
    
       @Override
       public void callRemoteEjb()  {   }
    }

    The annotation gives a way for the remote interface provider to tell the remote interface consumer that transactions are not needed, or are required, for a method, allowing the client to avoid or require outflow as needed.

    About the Author:

    Francesco Marchioni is a Senior Technical Account Manager on Middleware products based in Rome (Italy). Some of its contributions to the Community of JBoss developers are available on the site  http://www.mastertheboss.com

    References:

    https://source.redhat.com/?signin&r=%2Ftemporary_mojo%2Ftemp_personal_wiki%2Feap_71_new_ejb_client_tests_findings

    https://access.redhat.com/solutions/3061121


    Click here and quickly get started with Red Hat JBoss EAP download.

    Last updated: March 22, 2023

    Recent Posts

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.