Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Coding EJB clients for JBoss EAP 7.1

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

Share:

    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

    • More Essential AI tutorials for Node.js Developers

    • How to run a fraud detection AI model on RHEL CVMs

    • How we use software provenance at Red Hat

    • Alternatives to creating bootc images from scratch

    • How to update OpenStack Services on OpenShift

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    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

    Red Hat legal and privacy links

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

    Report a website issue