重复的局部变量会话(javax.jms.Session)

时间:2015-03-09 08:17:46

标签: java jsp session servlets jms

我正在尝试在我的jsp页面中向ActiveMQ发送一些消息。但我得到Session(javax.jms.Session)变量声明的问题。如果我在本地声明会话变量,我会得到(Duplicate local session variable session)问题。如果我在全局声明我的变量,我会得到(Type mismatch: cannot convert session to httpsession)错误。 我的代码,

<%@page import="org.apache.activemq.ActiveMQConnection" %>
<%@page import="org.apache.activemq.ActiveMQConnectionFactory" %>
<%@page import="javax.jms.Connection" %>
<%@page import="javax.jms.ConnectionFactory" %>
<%@page import="javax.jms.Destination" %>
<%@page import="javax.jms.JMSException" %>
<%@page import="javax.jms.MessageProducer" %>
<%@page import="javax.jms.Session" %>
<%@page import="javax.jms.TextMessage" %>


<%-- <%!
    private ConnectionFactory factory = null;
    private Connection connection = null;
    private Session session = null;
    private Destination destination = null;
    private MessageProducer producer = null;   

%> --%>


 <portlet:defineObjects />
 <theme:defineObjects />
 <%
     final ConnectionFactory factory = null;
     final Connection connection = null;
     final Session session = null;
     final Destination destination = null;
     final MessageProducer producer = null;  

         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(formData);
                producer.send(message);
                System.out.println("Sent: " + message);

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

但是相同的代码在servlet中运行良好。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

因为您定义了对象session两次:

<%-- <%!
    private ConnectionFactory factory = null;
    private Connection connection = null;
    private Session session = null; //<-- HERE

在这里

<theme:defineObjects />
 <%
     final ConnectionFactory factory = null;
     final Connection connection = null;
     final Session session = null; // <-- HERE

删除其中至少一个或重命名一个本地变量的会话。

我认为在你的servlet中你有一个成员和一个绝对有效的局部变量。