Wednesday, November 5, 2025

Weblogic 14.1.2 EJB client with JNDI lookup

 HOWTO



Git


Blog

 

 Add t3 client  

dependency>
    <groupId>com.oracle.weblogic</groupId>
    <artifactId>wlthint3client</artifactId>
    <version>14.1.2-0-0</version>
</dependency>

JNDI names

     java:global/basicWebappEjb/AccountManagerEJB!dave.service.AccountManagerRemote

Trying JNDI name: java:global/basicWebappEjb/AccountManagerEJB!dave.service.AccountManagerRemote
✓ Successfully looked up AccountManagerRemote EJB at: java:global/basicWebappEjb/AccountManagerEJB!dave.service.AccountManagerRemote
Depositing $100.5 to account: john.doe

EJB

package dave.service;

import javax.ejb.Stateless;
import javax.ejb.Remote;
import javax.ejb.Local;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.annotation.sql.DataSourceDefinition;

import dave.entity.Account;

/**
 * Option 1: Use name attribute to define the EJB name
 * The JNDI name will be: java:global/<app-name>/<ejb-name>!<interface>
 * 
 * Option 2: Use mappedName for explicit JNDI binding (WebLogic-specific)
 * mappedName = "ejb/AccountManager"
 */
@Stateless(name = "AccountManagerEJB")
@Local(AccountManager.class)
@Remote(AccountManagerRemote.class)
//Data Source defined for JPA. It assume the derby database is started up and listen to localhost:1527
@DataSourceDefinition(name = "java:module/env/mavenArchetypeDataSource", className = "org.apache.derby.jdbc.ClientXADataSource", portNumber = 1527, serverName = "localhost", databaseName = "examples", user = "examples", password = "examples", properties={"create=true", "weblogic.TestTableName=SQL SELECT 1 FROM SYS.SYSTABLES"})
public class AccountManagerImpl implements AccountManager, AccountManagerRemote {

  @PersistenceContext
  private EntityManager em;
  
  public void depositOnAccount(String name, float amount) {
    Account account = em.find(Account.class, name);
    if (account == null) {
      account = new Account();
      account.setName(name);
    }
    account.setAmount(account.getAmount() + amount);
    em.persist(account);
  }
  
  public Account findAccount(String name) {
    return em.find(Account.class, name);
  }
}

JNDI EJB client

/**
 * External Java EJB Client for AccountManager
 */
package dave.client;

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import dave.service.AccountManagerRemote;
import dave.entity.Account;

public class AccountManagerClient {

    private static final String WEBLOGIC_JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
    private static final String PROVIDER_URL = "t3://localhost:7001";
    
    /**
     * JNDI lookup patterns for WebLogic:
     * 
     * Pattern 1 (Portable Global JNDI):
     * java:global/<app-name>/<module-name>/<bean-name>!<fully-qualified-interface-name>
     * Example: java:global/basicWebappEjb/AccountManagerImpl!dave.service.AccountManagerRemote
     * 
     * Pattern 2 (WebLogic-specific):
     * <ejb-name>#<fully-qualified-interface-name>
     * Example: AccountManagerImpl#dave.service.AccountManagerRemote
     * 
     * Pattern 3 (Application-scoped):
     * java:app/<module-name>/<bean-name>!<fully-qualified-interface-name>
     */
    // Multiple JNDI names to try - WebLogic registers EJBs with multiple patterns
    private static final String[] JNDI_NAMES = {
        // Using custom name="AccountManagerEJB"
        "java:global/basicWebappEjb/AccountManagerEJB!dave.service.AccountManagerRemote",
        "java:global/basicWebappEjb/AccountManagerEJB",
        // Fallback to default names
        "java:global/basicWebappEjb/AccountManagerImpl!dave.service.AccountManagerRemote",
        "java:global/basicWebappEjb/AccountManagerImpl",
        "basicWebappEjb/AccountManagerEJB!dave.service.AccountManagerRemote",
        "ejb/AccountManager#dave.service.AccountManagerRemote"
    };
    
    public static void main(String[] args) {
        AccountManagerClient client = new AccountManagerClient();
        try {
            client.testEJB();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void testEJB() throws NamingException {
        Context context = null;
        try {
            // Get initial context
            context = getInitialContext();
            
            // Try multiple JNDI names
            AccountManagerRemote accountManager = null;
            String successfulJndiName = null;
            
            for (String jndiName : JNDI_NAMES) {
                try {
                    System.out.println("Trying JNDI name: " + jndiName);
                    accountManager = (AccountManagerRemote) context.lookup(jndiName);
                    successfulJndiName = jndiName;
                    System.out.println("✓ Successfully looked up AccountManagerRemote EJB at: " + jndiName);
                    break;
                } catch (NamingException e) {
                    System.out.println("✗ Failed: " + e.getMessage());
                }
            }
            
            if (accountManager == null) {
                throw new NamingException("Could not find EJB with any of the attempted JNDI names");
            }
            
            // Test deposit
            String accountName = "john.doe";
            float depositAmount = 100.50f;
            
            System.out.println("Depositing $" + depositAmount + " to account: " + accountName);
            accountManager.depositOnAccount(accountName, depositAmount);
            
            // Test find
            Account account = accountManager.findAccount(accountName);
            if (account != null) {
                System.out.println("Account found: " + account.getName() + 
                                   ", Balance: $" + account.getAmount());
            } else {
                System.out.println("Account not found");
            }
            
        } finally {
            if (context != null) {
                try {
                    context.close();
                } catch (NamingException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * Create InitialContext for WebLogic Server
     */
    private Context getInitialContext() throws NamingException {
        Hashtable<String, String> env = new Hashtable<>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, WEBLOGIC_JNDI_FACTORY);
        env.put(Context.PROVIDER_URL, PROVIDER_URL);
        
        // Add credentials - required for WebLogic security
        env.put(Context.SECURITY_PRINCIPAL, "weblogic");
        env.put(Context.SECURITY_CREDENTIALS, "weblogic123");
        
        return new InitialContext(env);
    }
}

Run client from Maven 


dave@fedora:/git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project$ mvn compile exec:java -Dexec.mainClass="./src/main/java/dave/client/AccountManagerClient.java"
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/3.1.0/exec-maven-plugin-3.1.0.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/3.1.0/exec-maven-plugin-3.1.0.pom (14 kB at 24 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/mojo-parent/69/mojo-parent-69.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/mojo-parent/69/mojo-parent-69.pom (35 kB at 569 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/3.1.0/exec-maven-plugin-3.1.0.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/3.1.0/exec-maven-plugin-3.1.0.jar (73 kB at 982 kB/s)
[INFO] 
[INFO] -----------------&lt; dave:dave-basic-webapp-ejb-project &gt;-----------------
[INFO] Building basicWebappEjb 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- resources:3.3.1:resources (default-resources) @ dave-basic-webapp-ejb-project ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource from src/main/resources to target/classes
[INFO] 
[INFO] --- compiler:2.3.2:compile (default-compile) @ dave-basic-webapp-ejb-project ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- exec:3.1.0:java (default-cli) @ dave-basic-webapp-ejb-project ---
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.3/commons-exec-1.3.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.3/commons-exec-1.3.pom (11 kB at 200 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-parent/35/commons-parent-35.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-parent/35/commons-parent-35.pom (58 kB at 1.2 MB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.3/commons-exec-1.3.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.3/commons-exec-1.3.jar (54 kB at 990 kB/s)
Trying JNDI name: java:global/basicWebappEjb/AccountManagerEJB!dave.service.AccountManagerRemote
✓ Successfully looked up AccountManagerRemote EJB at: java:global/basicWebappEjb/AccountManagerEJB!dave.service.AccountManagerRemote
Depositing $100.5 to account: john.doe


Running with debug

directory /git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project/src/main/resources
excludes []
includes []
[DEBUG] ignoreDelta true
[INFO] Copying 1 resource from src/main/resources to target/classes
[DEBUG] Copying file META-INF/persistence.xml
[DEBUG] file persistence.xml has a filtered file extension
[DEBUG] Using 'null' encoding to copy filtered resource 'persistence.xml'.
[DEBUG] copy /git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project/src/main/resources/META-INF/persistence.xml to /git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project/target/classes/META-INF/persistence.xml
[DEBUG] no user filter components
[INFO] 
[INFO] --- compiler:2.3.2:compile (default-compile) @ dave-basic-webapp-ejb-project ---
[DEBUG] Using mirror maven-default-http-blocker (http://0.0.0.0/) for apache.snapshots (http://people.apache.org/repo/m2-snapshot-repository).
[DEBUG] Using mirror maven-default-http-blocker (http://0.0.0.0/) for codehaus.snapshots (http://snapshots.repository.codehaus.org).
[DEBUG] Using mirror maven-default-http-blocker (http://0.0.0.0/) for snapshots (http://snapshots.maven.codehaus.org/maven2).
[DEBUG] Using mirror maven-default-http-blocker (http://0.0.0.0/) for central (http://repo1.maven.org/maven2).
[DEBUG] Dependency collection stats {ConflictMarker.analyzeTime=227695, ConflictMarker.markTime=109858, ConflictMarker.nodeCount=101, ConflictIdSorter.graphTime=103733, ConflictIdSorter.topsortTime=20479, ConflictIdSorter.conflictIdCount=27, ConflictIdSorter.conflictIdCycleCount=0, ConflictResolver.totalTime=1003151, ConflictResolver.conflictItemCount=49, DfDependencyCollector.collectTime=66446805, DfDependencyCollector.transformTime=1481816}
[DEBUG] org.apache.maven.plugins:maven-compiler-plugin:jar:2.3.2
[DEBUG]    org.apache.maven:maven-plugin-api:jar:2.0.6:compile
[DEBUG]    org.apache.maven:maven-artifact:jar:2.0.6:compile
[DEBUG]    org.apache.maven:maven-core:jar:2.0.6:compile
[DEBUG]       org.apache.maven:maven-settings:jar:2.0.6:compile
[DEBUG]       org.apache.maven:maven-plugin-parameter-documenter:jar:2.0.6:compile
[DEBUG]       org.apache.maven:maven-profile:jar:2.0.6:compile
[DEBUG]       org.apache.maven:maven-model:jar:2.0.6:compile
[DEBUG]       org.apache.maven:maven-repository-metadata:jar:2.0.6:compile
[DEBUG]       org.apache.maven:maven-error-diagnostics:jar:2.0.6:compile
[DEBUG]       org.apache.maven:maven-project:jar:2.0.6:compile
[DEBUG]          org.apache.maven:maven-plugin-registry:jar:2.0.6:compile
[DEBUG]       org.apache.maven:maven-plugin-descriptor:jar:2.0.6:compile
[DEBUG]       org.apache.maven:maven-artifact-manager:jar:2.0.6:compile
[DEBUG]       org.apache.maven:maven-monitor:jar:2.0.6:compile
[DEBUG]    org.apache.maven:maven-toolchain:jar:1.0:compile
[DEBUG]    org.codehaus.plexus:plexus-utils:jar:2.0.5:compile
[DEBUG]    org.codehaus.plexus:plexus-compiler-api:jar:1.8.1:compile
[DEBUG]    org.codehaus.plexus:plexus-compiler-manager:jar:1.8.1:compile
[DEBUG]    org.codehaus.plexus:plexus-compiler-javac:jar:1.8.1:runtime
[DEBUG] Created new class realm plugin>org.apache.maven.plugins:maven-compiler-plugin:2.3.2
[DEBUG] Importing foreign packages into class realm plugin>org.apache.maven.plugins:maven-compiler-plugin:2.3.2
[DEBUG]   Imported:  < maven.api
[DEBUG] Populating class realm plugin>org.apache.maven.plugins:maven-compiler-plugin:2.3.2
[DEBUG]   Included: org.apache.maven.plugins:maven-compiler-plugin:jar:2.3.2
[DEBUG]   Included: org.codehaus.plexus:plexus-utils:jar:2.0.5
[DEBUG]   Included: org.codehaus.plexus:plexus-compiler-api:jar:1.8.1
[DEBUG]   Included: org.codehaus.plexus:plexus-compiler-manager:jar:1.8.1
[DEBUG]   Included: org.codehaus.plexus:plexus-compiler-javac:jar:1.8.1
[DEBUG] Loading mojo org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-compiler-plugin:2.3.2, parent: jdk.internal.loader.ClassLoaders$AppClassLoader@5ffd2b27]
[DEBUG] Configuring mojo execution 'org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile:default-compile' with basic configurator -->
[DEBUG]   (f) basedir = /git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project
[DEBUG]   (f) buildDirectory = /git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project/target
[DEBUG]   (f) classpathElements = [/git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project/target/classes, /home/dave/.m2/repository/javax/javaee-web-api/6.0/javaee-web-api-6.0.jar, /app/weblogic/weblogic-14.1.2/wlserver/server/lib/wlthint3client.jar, /app/weblogic/weblogic-14.1.2/wlserver/server/lib/weblogic.jar]
[DEBUG]   (f) compileSourceRoots = [/git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project/src/main/java]
[DEBUG]   (f) compilerId = javac
[DEBUG]   (f) debug = true
[DEBUG]   (f) failOnError = true
[DEBUG]   (f) fork = false
[DEBUG]   (f) generatedSourcesDirectory = /git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project/target/generated-sources/annotations
[DEBUG]   (f) optimize = false
[DEBUG]   (f) outputDirectory = /git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project/target/classes
[DEBUG]   (f) outputFileName = basicWebappEjb
[DEBUG]   (f) projectArtifact = dave:dave-basic-webapp-ejb-project:war:1.0-SNAPSHOT
[DEBUG]   (f) session = org.apache.maven.execution.MavenSession@588307f7
[DEBUG]   (f) showDeprecation = false
[DEBUG]   (f) showWarnings = false
[DEBUG]   (f) source = 1.8
[DEBUG]   (f) staleMillis = 0
[DEBUG]   (f) target = 1.8
[DEBUG]   (f) verbose = false
[DEBUG] -- end configuration --
[DEBUG] Using compiler 'javac'.
[DEBUG] Source directories: [/git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project/src/main/java]
[DEBUG] Classpath: [/git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project/target/classes
 /home/dave/.m2/repository/javax/javaee-web-api/6.0/javaee-web-api-6.0.jar
 /app/weblogic/weblogic-14.1.2/wlserver/server/lib/wlthint3client.jar
 /app/weblogic/weblogic-14.1.2/wlserver/server/lib/weblogic.jar]
[DEBUG] Output directory: /git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project/target/classes
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- exec:3.1.0:java (default-cli) @ dave-basic-webapp-ejb-project ---
[DEBUG] Dependency collection stats {ConflictMarker.analyzeTime=23194, ConflictMarker.markTime=30654, ConflictMarker.nodeCount=4, ConflictIdSorter.graphTime=5608, ConflictIdSorter.topsortTime=10155, ConflictIdSorter.conflictIdCount=4, ConflictIdSorter.conflictIdCycleCount=0, ConflictResolver.totalTime=144167, ConflictResolver.conflictItemCount=4, DfDependencyCollector.collectTime=11120279, DfDependencyCollector.transformTime=226745}
[DEBUG] org.codehaus.mojo:exec-maven-plugin:jar:3.1.0
[DEBUG]    org.codehaus.plexus:plexus-utils:jar:3.4.2:compile
[DEBUG]    org.codehaus.plexus:plexus-component-annotations:jar:2.1.1:compile (optional)
[DEBUG]    org.apache.commons:commons-exec:jar:1.3:compile
[DEBUG] Created new class realm plugin>org.codehaus.mojo:exec-maven-plugin:3.1.0
[DEBUG] Importing foreign packages into class realm plugin>org.codehaus.mojo:exec-maven-plugin:3.1.0
[DEBUG]   Imported:  < maven.api
[DEBUG] Populating class realm plugin>org.codehaus.mojo:exec-maven-plugin:3.1.0
[DEBUG]   Included: org.codehaus.mojo:exec-maven-plugin:jar:3.1.0
[DEBUG]   Included: org.codehaus.plexus:plexus-utils:jar:3.4.2
[DEBUG]   Included: org.codehaus.plexus:plexus-component-annotations:jar:2.1.1
[DEBUG]   Included: org.apache.commons:commons-exec:jar:1.3
[DEBUG] Loading mojo org.codehaus.mojo:exec-maven-plugin:3.1.0:java from plugin realm ClassRealm[plugin>org.codehaus.mojo:exec-maven-plugin:3.1.0, parent: jdk.internal.loader.ClassLoaders$AppClassLoader@5ffd2b27]
[DEBUG] Configuring mojo execution 'org.codehaus.mojo:exec-maven-plugin:3.1.0:java:default-cli' with basic configurator -->
[DEBUG]   (f) addOutputToClasspath = true
[DEBUG]   (f) addResourcesToClasspath = false
[DEBUG]   (f) additionalClasspathElements = [/app/weblogic/weblogic-14.1.2/wlserver/server/lib/wlthint3client.jar]
[DEBUG]   (f) arguments = []
[DEBUG]   (f) classpathScope = runtime
[DEBUG]   (f) cleanupDaemonThreads = false
[DEBUG]   (f) daemonThreadJoinTimeout = 15000
[DEBUG]   (f) includePluginDependencies = false
[DEBUG]   (f) includeProjectDependencies = true
[DEBUG]   (f) keepAlive = false
[DEBUG]   (f) killAfter = -1
[DEBUG]   (f) mainClass = dave.client.AccountManagerClient
[DEBUG]   (f) pluginDependencies = [org.codehaus.mojo:exec-maven-plugin:maven-plugin:3.1.0:, org.codehaus.plexus:plexus-utils:jar:3.4.2:compile, org.codehaus.plexus:plexus-component-annotations:jar:2.1.1:compile, org.apache.commons:commons-exec:jar:1.3:compile]
[DEBUG]   (f) preloadCommonPool = 0
[DEBUG]   (f) project = MavenProject: dave:dave-basic-webapp-ejb-project:1.0-SNAPSHOT @ /git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project/pom.xml
[DEBUG]   (f) session = org.apache.maven.execution.MavenSession@588307f7
[DEBUG]   (f) skip = false
[DEBUG]   (f) stopUnresponsiveDaemonThreads = false
[DEBUG] -- end configuration --
[DEBUG] Invoking : dave.client.AccountManagerClient.main()
[DEBUG] Plugin Dependencies will be excluded.
[DEBUG] Project Dependencies will be included.
[DEBUG] Collected project artifacts []
[DEBUG] Collected project classpath [/git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project/target/classes]
[DEBUG] Adding to classpath : /git/weblogic/dave-basic-project-14.1.2/dave-basic-webapp-ejb-project/target/classes
[DEBUG] Adding additional classpath element: /app/weblogic/weblogic-14.1.2/wlserver/server/lib/wlthint3client.jar to classpath
[DEBUG] joining on thread Thread[#34,dave.client.AccountManagerClient.main(),5,dave.client.AccountManagerClient]
Trying JNDI name: java:global/basicWebappEjb/AccountManagerEJB!dave.service.AccountManagerRemote
✓ Successfully looked up AccountManagerRemote EJB at: java:global/basicWebappEjb/AccountManagerEJB!dave.service.AccountManagerRemote
Depositing $100.5 to account: john.doe


Check JNDI in remote Weblogic 14.1.2 console 

 

 

No comments:

Post a Comment