gpt4 book ai didi

java - @EJB 注释导致 servlet 停止工作

转载 作者:太空宇宙 更新时间:2023-11-04 11:35:37 25 4
gpt4 key购买 nike

我有以下 EJB 实现一个简单的 ActiveMQ 消息队列。 ActiveMQ 代码本身运行得很好。这是部署在 TomEE 服务器上的。

SenderBean 类:

package Beans;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

@LocalBean
@Stateless
public class SenderBean {

private ConnectionFactory factory = null;
private Connection connection = null;
private Session session = null;
private Destination destination = null;
private MessageProducer producer = null;

public SenderBean() {

}

public void sendMessage() {

try {
factory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_BROKER_URL);
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("SAMPLEQUEUE");
producer = session.createProducer(destination);
TextMessage message = session.createTextMessage();
message.setText("Hello ...This is a sample message..sending from FirstClient");
producer.send(message);
System.out.println("Sent: " + message.getText());

connection.close();

} catch (JMSException e) {
e.printStackTrace();
}
}
}

我还有以下类尝试在 servlet 中使用 bean。如果我包含 @EJB 注释(如下面的示例所示),servlet 在接收到 post 时会抛出异常。但是,如果我删除注释并使用 new SenderBean() 实例化 bean 类,则一切都会按预期运行。造成这种情况的原因是什么?

SenderServlet 类:

package Servlets;

import Beans.ReceiverBean;
import Beans.SenderBean;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "SenderServlet")
public class SenderServlet extends HttpServlet {

@EJB
SenderBean senderBean;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
senderBean.sendMessage();
}
}

500 错误后,HttpServletResponse 中收到异常:

HTTP Status 500 - Error instantiating servlet class Servlets.SenderServlet</h1><div class="line"></div><p><b>type</b> Exception report</p><p><b>message</b> <u>Error instantiating servlet class Servlets.SenderServlet</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b></p><pre>javax.servlet.ServletException: Error instantiating servlet class Servlets.SenderServlet
org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:44)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.tomee.catalina.OpenEJBSecurityListener$RequestCapturer.invoke(OpenEJBSecurityListener.java:97)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

root cause javax.naming.NameNotFoundException: Name [Servlets.SenderServlet/senderBean] is not bound in this Context. Unable to find [Servlets.SenderServlet].
org.apache.naming.NamingContext.lookup(NamingContext.java:816)
org.apache.naming.NamingContext.lookup(NamingContext.java:173)
org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:44)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.tomee.catalina.OpenEJBSecurityListener$RequestCapturer.invoke(OpenEJBSecurityListener.java:97)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

爆炸的 Artifact : enter image description here

最佳答案

您是否与 activemq 或 hibernate 有类路径冲突?提供的库不在 WEB-INF/lib 中吗?

关于java - @EJB 注释导致 servlet 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43328141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com