1

我在尝试查找 TimerManager 时收到 javax.naming.NameNotFoundException。在这里,我给出了 MDB 组件。

MDBTimer.java

package my.examples.mdb.timer;

import javax.ejb.MessageDriven;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import commonj.timers.TimerManager;

@MessageDriven(mappedName = "TEST_Q",name = "MyTimerMDB",activationConfig = 
{
    @javax.ejb.ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @javax.ejb.ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @javax.ejb.ActivationConfigProperty(propertyName = "transactionType", propertyValue = "Container") })
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class MDBTimer  implements MessageListener {

private static TimerManager timerManager = null;

@Override
public void onMessage(Message arg0) {
    System.out.println("onMessage() method called...\n\n");

    if (arg0 instanceof ObjectMessage) {

        ObjectMessage msg = (ObjectMessage) arg0;
        try {

            if (msg.getObject() instanceof String) {
                System.out.println("Message received >> "+msg.getObject());
            }
        }catch (Exception e){

        }
    }
    if(timerManager==null) {


        try {
            InitialContext ctx = new InitialContext();
            timerManager = (TimerManager)
                ctx.lookup("java:comp/env/timer/MyDefaultTimer");
            /*timerManager = (TimerManager)
            ctx.lookup("timer/MyDefaultTimer");*/
        } catch (NamingException e) {               
            e.printStackTrace();
        }


    } 
    System.out.println("onMessage() method returned...\n\n");
}

}

ejb-jar.xml

<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                    http://java.sun.com/xml/ns/j2ee/ejb-jar_3_0.xsd"
version="3.0">
<enterprise-beans>
    <message-driven>
        <ejb-name>MyTimerMDB</ejb-name>
        <ejb-class>my.examples.mdb.timer.MDBTimer
        </ejb-class>        
        <resource-ref>
            <description>My Default Timer Manager</description>
            <res-ref-name>timer/MyDefaultTimer</res-ref-name>
            <res-type>commonj.timer.TimerManager</res-type>
            <res-auth>Container</res-auth>
            <res-sharing-scope>Unshareable</res-sharing-scope>
        </resource-ref>         
    </message-driven>
</enterprise-beans>

发布消息时的 Weblogic 控制台输出。

Message received >> This is test message!!! 
javax.naming.NameNotFoundException: While trying to look up  comp/env/timer/MyDefaultTimer in /app/ejb/MyTimerMDB.jar#MyTimerMDB.; remaining  name 'comp/env/timer/MyDefaultTimer'
       at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException (BasicNamingNode.java:1139)
    at weblogic.jndi.internal.ApplicationNamingNode.lookup(ApplicationNamingNode.java:144)
    at weblogic.jndi.internal.WLEventContextImpl.lookup(WLEventContextImpl.java:254)
    at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:380)
    at weblogic.jndi.factories.java.ReadOnlyContextWrapper.lookup(ReadOnlyContextWrapper.java:45)
    at weblogic.jndi.internal.AbstractURLContext.lookup(AbstractURLContext.java:130)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at my.examples.mdb.timer.MDBTimer.onMessage(MDBTimer.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.bea.core.repackaged.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:281)
    at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:187)
    at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:154)
    at com.bea.core.repackaged.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:126)
    at com.bea.core.repackaged.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:114)
    at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
    at com.bea.core.repackaged.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
    at com.bea.core.repackaged.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:126)
    at com.bea.core.repackaged.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:114)
    at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
    at com.bea.core.repackaged.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)
    at $Proxy89.onMessage(Unknown Source)
    at weblogic.ejb.container.internal.MDListener.execute(MDListener.java:466)
    at weblogic.ejb.container.internal.MDListener.transactionalOnMessage(MDListener.java:371)
    at weblogic.ejb.container.internal.MDListener.onMessage(MDListener.java:327)
    at weblogic.jms.client.JMSSession.onMessage(JMSSession.java:4547)
    at weblogic.jms.client.JMSSession.execute(JMSSession.java:4233)
    at weblogic.jms.client.JMSSession.executeMessage(JMSSession.java:3709)
    at weblogic.jms.client.JMSSession.access$000(JMSSession.java:114)
    at weblogic.jms.client.JMSSession$UseForRunnable.run(JMSSession.java:5058)
    at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:516)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
  onMessage() method returned...

任何人都可以帮助解决这个问题

提前致谢 :)

4

1 回答 1

0

我在 ejb-jar.xml 中犯了一个愚蠢的错误。我给出了错误的资源类型,例如 commonj.timer.TimerManager。实际名称应该是commonj.timers.TimerManager

在这里,我给出了更新的 ejb-jar.xml 文件。

        <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                            http://java.sun.com/xml/ns/j2ee/ejb-jar_3_0.xsd"
        version="3.0">
        <enterprise-beans>
            <message-driven>
                <ejb-name>MyTimerMDB</ejb-name>
                <ejb-class>my.examples.mdb.timer.MDBTimer
                </ejb-class>
                <resource-ref>
                    <description>My Default Timer Manager</description>
                    <res-ref-name>timer/MyDefaultTimer</res-ref-name>
                    <res-type>commonj.timers.TimerManager</res-type>
                    <res-auth>Container</res-auth>
                    <res-sharing-scope>Unshareable</res-sharing-scope>
                </resource-ref>
            </message-driven>
        </enterprise-beans>
    </ejb-jar>

现在我可以成功查找 TimerMagers

谢谢!!!!!

于 2010-08-12T08:31:47.537 回答