简体   繁体   中英

How Do I Correctly Deploy A Bean - JNDI, Weblogic 12C

I am trying to create a bean for a weblogic 12C server. The bean is just going to hold a couple values that at least one (possibly more) application(s) can reference (specifically - a bearer token, the duration of the token, and when it was retrieved)

I'm kind of new to this (hence my boggle)

I am running the latest version of Eclipse and have installed the Weblogic tools. I build my bean and deployed it to the local server. At this point, I am just trying to get my context and lookup my bean. When I run the client all I get this:

java.lang.ClassCastException: weblogic.jndi.internal.WLContextImpl cannot be cast to com.salientcrgt.chums.oAuthToken
    at TestRemoteLoading.TestClass.getBean(TestClass.java:46)
    at TestRemoteLoading.TestClass.main(TestClass.java:27)

If I go the the Weblogic console and look up my deployment, I see this in the "Test" tab:

oAuthTokenBeanEAR oAuthTokenBeanEAR     
Classloader Analysis Tool   Classloader Analysis Tool on server AdminServer
oAuthToken 
oAuthToken      Test points for this EJB module.
oAuthToken  AdminServer No test point since this EJB's home does not have a JNDI name.

BEAN PROJECT:

ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/ejb-jar_3_2.xsd" version="3.2">
  <display-name>oAuthTokenBean </display-name>
  <ejb-client-jar>oAuthTokenBeanClient.jar</ejb-client-jar>
</ejb-jar>

weblogic-ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-ejb-jar xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-ejb-jar" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd http://xmlns.oracle.com/weblogic/weblogic-ejb-jar http://xmlns.oracle.com/weblogic/weblogic-ejb-jar/1.7/weblogic-ejb-jar.xsd">
    <!--weblogic-version:12.2.1.3-->
    <wls:weblogic-enterprise-bean>
        <wls:ejb-name>oAuthToken</wls:ejb-name>
        <wls:stateless-session-descriptor></wls:stateless-session-descriptor>
        <wls:jndi-name>oAuthToken</wls:jndi-name>
        <wls:local-jndi-name>oAuthToken</wls:local-jndi-name>
        <wls:jndi-binding>
            <wls:class-name>com.salientcrgt.chums.oAuthToken</wls:class-name>
        </wls:jndi-binding>
    </wls:weblogic-enterprise-bean>
</wls:weblogic-ejb-jar>

oAuthToken.java

/**
 * Session Bean implementation class oAuthToken
 */
@Stateless(name = "oAuthToken", mappedName = "oAuthToken")
@Remote ({com.salientcrgt.chums.oAuthTokenOBJ.class})
public class oAuthToken implements oAuthTokenOBJ, Serializable {

    public oAuthToken() {
    }

    private static final long serialVersionUID = 1L;

    private String service = "default";
    private String bearerToken;
    private int duration;
    private Date timeStamp;
...

oAuthtokenOBJ.java

package com.salientcrgt.chums;

import java.util.Date;

public interface oAuthTokenOBJ {
    public String getService();
    public String getBearerToken();
    public int getDuration();
    public Date getTimeStamp();
    public boolean Expired();
}

BEAN EAR PROJECT

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-application xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-application" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/javaee_6.xsd http://xmlns.oracle.com/weblogic/weblogic-application http://xmlns.oracle.com/weblogic/weblogic-application/1.8/weblogic-application.xsd">
    <!--weblogic-version:12.2.1.3-->
    <wls:application-param>
        <wls:param-name>webapp.encoding.default</wls:param-name>
        <wls:param-value>UTF-8</wls:param-value>
    </wls:application-param>
</wls:weblogic-application>

Client Application:

Here are the methods where I am trying to get the bean

   @SuppressWarnings("unchecked")
    private static Context getInitialContext() throws NamingException {
        @SuppressWarnings("rawtypes")
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7001");
        return new InitialContext(env);
    }

    public static void getBean() {
        try {
            final Context c = getInitialContext();
            /** print list of jndi names in context */
            NamingEnumeration<NameClassPair> list = c.list("");
            while (list.hasMore()) {
              System.out.println(list.next().getName());
            }
            // get the bean
            setTokenBean((oAuthToken) c.lookup("oAuthToken#com"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

I think I am doing the correct thing in the client but have not assembled/deployed the bean correctly in some way. I hope this is enough information to show what I have screwed up here.

This produced the following output:

mejbmejb_jarMejb_EO
oAuthToken#com
oAuthTokenBeanEARoAuthTokenBean_jaroAuthToken_Home
jmx
javax
oAuthTokenBeanEARoAuthTokenBean_jaroAuthToken_oAuthTokenOBJ
weblogic
__WL_GlobalJavaApp
_WL_internal_0N2TL1pYAVfZYdsOf5bIz5NXR1UAnTtG7gYEw8eSpFDasIK4NdrDolV9ZtejElAL
ejb
java:global
eis
java.lang.ClassCastException: weblogic.jndi.internal.WLContextImpl cannot be cast to com.salientcrgt.chums.oAuthToken
    at TestRemoteLoading.TestClass.getBean(TestClass.java:46)
    at TestRemoteLoading.TestClass.main(TestClass.java:27)

Got it sorted.

The above code is mostly correct. Here is what I ended up with:

    public static void getBean() {
        try {
            final Context c = getInitialContext();
            setTokenBean( (oAuthTokenOBJ) 
                   c.lookup("oAuthToken#com.salientcrgt.chums.oAuthTokenOBJ"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

oAuthTokenOBJ is the interface.

I went to the main in my test class and did this:

    public static void main(String Arg[]) throws IOException  {
        getBean();
        String myService = tokenBean.getService();
        System.out.println(myService);
    }

It prints "default", which is the initial value in the bean for that field. Hope this helps someone else.

(I can't be the only one who finds Oracle documentation to be a smidge obtuse.) lol

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM