You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过父类EJB名称查找其子类EJB?JBOSS 4.2.3场景求助

Look Up Subclass EJB via Parent EJB Details (JBoss 4.2.3)

Alright, let's break down how to solve this problem. You've got a parent EJB MyCoreEjb and a subclass MyCustomizationEjb packaged together in the same JAR on JBoss 4.2.3, and you can't modify the parent project. Here are two actionable approaches to find the subclass EJB using parent-related identifiers:

Scenario 1: Find Subclass Instances via Parent Interface

Since MyCustomizationEjb inherits from MyCoreEjb, it implicitly implements the parent's interfaces (MyCore/MyCoreLocal) — but JBoss 4.2.3 doesn't automatically bind the subclass to the parent's JNDI names unless explicitly configured. Here's how to work around that:

Option 1: Explicitly Bind Subclass to Parent Interfaces

You can update the subclass EJB's annotations to include the parent's interfaces in its @Local/@Remote declarations, plus use JBoss-specific binding annotations to map it to the parent's JNDI names. This lets you look up the subclass using the parent's interface names directly:

@Stateless
// Add parent interfaces to local/remote views
@Local({MyCustomizationLocal.class, MyCoreLocal.class})
@Remote({MyCustomization.class, MyCore.class})
// Bind to parent's remote JNDI name (adjust for local if needed)
@org.jboss.ejb3.annotation.RemoteBinding(jndiBinding = "MyCore/remote")
public class MyCustomizationEjb extends MyCoreEjb implements MyCustomizationLocal, MyCustomization {
    // Your subclass logic here
}

If annotations don't take effect, you can manually configure the binding in jboss.xml (packaged with your JAR):

<enterprise-beans>
  <session>
    <ejb-name>MyCustomizationEjb</ejb-name>
    <jndi-name>MyCore/remote</jndi-name>
    <local-jndi-name>MyCore/local</local-jndi-name>
  </session>
</enterprise-beans>

Now, when you run ctx.lookup("MyCore/remote"), you'll get an instance of MyCustomizationEjb instead of the parent. Just make sure the parent EJB isn't using the same JNDI name (if it is, adjust the subclass's binding to a unique alias like MyCoreCustom/remote).

Option 2: Traverse JNDI Context to Find Subclasses

If you can't modify the subclass annotations (or prefer a dynamic approach), you can traverse JBoss's JNDI tree to find all EJBs that implement the parent interface and exclude the parent itself:

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.NameClassPair;
import java.util.ArrayList;
import java.util.List;

public class EJBSubclassFinder {
    public static List<MyCore> getMyCoreSubclasses() throws Exception {
        Context jndiCtx = new InitialContext();
        List<MyCore> subclassEjbs = new ArrayList<>();
        
        // Adjust the context path if needed — JBoss 4.2.3 typically binds remote EJBs at the root or "java:comp/env/ejb"
        NamingEnumeration<NameClassPair> jndiEntries = jndiCtx.list("");
        
        while (jndiEntries.hasMoreElements()) {
            NameClassPair entry = jndiEntries.nextElement();
            String jndiName = entry.getName();
            
            try {
                Object ejbInstance = jndiCtx.lookup(jndiName);
                // Check if it's a subclass of MyCore but not the parent itself
                if (ejbInstance instanceof MyCore && !(ejbInstance instanceof MyCoreEjb)) {
                    subclassEjbs.add((MyCore) ejbInstance);
                }
            } catch (Exception e) {
                // Skip entries that aren't valid EJBs
                continue;
            }
        }
        return subclassEjbs;
    }
}

Note: This approach is less efficient but works if you need to dynamically discover all subclasses. Use JBoss's built-in jndi-view tool to verify the JNDI path if you run into issues.

Scenario 2: Look Up Subclass via Parent EJB's JNDI Name

If you specifically need to use the parent EJB's exact JNDI name (e.g., MyCoreEjb/remote) to get the subclass, you'll need to override the parent's binding with the subclass. Warning: This will replace the parent EJB's JNDI entry, so only use this if you don't need to access the parent EJB directly anymore.

Add this to the subclass's annotations:

@org.jboss.ejb3.annotation.RemoteBinding(jndiBinding = "MyCoreEjb/remote")

Or update jboss.xml as shown earlier with the parent's JNDI name.

Key Notes

  • Ensure both EJBs are packaged in the same JAR and deployed correctly — JBoss 4.2.3 scans the entire JAR for EJB annotations.
  • Use JBoss's jndi-view console (usually at http://your-server:8080/jmx-console) to inspect JNDI bindings and confirm your configurations are working.

内容的提问来源于stack exchange,提问作者André Queiroz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:52:02