JavaEE教程不起作用 - 迷失在异常的海洋中

时间:2018-05-03 22:03:53

标签: java java-ee jms

我正在尝试在以下网址上执行简单的JMS Java EE示例:https://javaee.github.io/tutorial/jms-examples003.html#GKTJS我一直没事,直到这一部分:https://javaee.github.io/tutorial/jms-examples003.html#to-run-the-producer-client

问题发生时执行的代码如下:

/**
 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
 *
 * You may not modify, use, reproduce, or distribute this software except in
 * compliance with  the terms of the License at:
 * https://github.com/javaee/tutorial-examples/LICENSE.txt
 */
package javaeetutorial.producer;

import javax.annotation.Resource;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSContext;
import javax.jms.JMSRuntimeException;
import javax.jms.Queue;
import javax.jms.Topic;

/**
 * The Producer class consists only of a main method, which sends several
 * messages to a queue or topic.
 *
 * Run this program in conjunction with SynchConsumer or AsynchConsumer. Specify
 * "queue" or "topic" on the command line when you run the program. By default,
 * the program sends one message. Specify a number after the destination name to
 * send that number of messages.
 */
public class Producer {

    @Resource(lookup = "java:comp/DefaultJMSConnectionFactory")
    private static ConnectionFactory connectionFactory;
    @Resource(lookup = "jms/MyQueue")
    private static Queue queue;
    @Resource(lookup = "jms/MyTopic")
    private static Topic topic;

    /**
     * Main method.
     *
     * @param args the destination used by the example and, optionally, the
     * number of messages to send
     */
    public static void main(String[] args) {
        final int NUM_MSGS;

        if ((args.length < 1) || (args.length > 2)) {
            System.err.println(
                    "Program takes one or two arguments: "
                    + "<dest_type> [<number-of-messages>]");
            System.exit(1);
        }

        String destType = args[0];
        System.out.println("Destination type is " + destType);

        if (!(destType.equals("queue") || destType.equals("topic"))) {
            System.err.println("Argument must be \"queue\" or " + "\"topic\"");
            System.exit(1);
        }

        if (args.length == 2) {
            NUM_MSGS = (new Integer(args[1])).intValue();
        } else {
            NUM_MSGS = 1;
        }

        Destination dest = null;

        try {
            if (destType.equals("queue")) {
                dest = (Destination) queue;
            } else {
                dest = (Destination) topic;
            }
        } catch (JMSRuntimeException e) {
            System.err.println("Error setting destination: " + e.toString());
            System.exit(1);
        }

        /*
         * Within a try-with-resources block, create context.
         * Create producer and message.
         * Send messages, varying text slightly.
         * Send end-of-messages message.
         */
        try (JMSContext context = connectionFactory.createContext();) {
            int count = 0;

            for (int i = 0; i < NUM_MSGS; i++) {
                String message = "This is message " + (i + 1) 
                        + " from producer";
                // Comment out the following line to send many messages
                System.out.println("Sending message: " + message);
                context.createProducer().send(dest, message);
                count += 1;
            }
            System.out.println("Text messages sent: " + count);

            /*
             * Send a non-text control message indicating end of
             * messages.
             */
            context.createProducer().send(dest, context.createMessage());
            // Uncomment the following line if you are sending many messages
            // to two synchronous consumers
            // context.createProducer().send(dest, context.createMessage());
        } catch (JMSRuntimeException e) {
            System.err.println("Exception occurred: " + e.toString());
            System.exit(1);
        }
        System.exit(0);
    }
}

还有一个glassfish-resources.xml文件,其中包含以下代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
  <admin-object-resource enabled="true" jndi-name="jms/MyQueue" object-type="user" res-adapter="jmsra" res-type="javax.jms.Queue">
    <description/>
    <property name="Name" value="PhysicalQueue"/>
  </admin-object-resource>
  <admin-object-resource enabled="true" jndi-name="jms/MyTopic" object-type="user" res-adapter="jmsra" res-type="javax.jms.Topic">
    <description/>
    <property name="Name" value="PhysicalTopic"/>
  </admin-object-resource>
</resources>

此XML文件使用如下:

asadmin add-resources src/main/setup/glassfish-resources.xml

此命令可创建队列和主题目标。我可以在GlassFish管理控制台中查看它们。请看下面的截图如下: enter image description here

程序运行如下:

appclient -client target/producer.jar queue 3

此时我收到了所有这些错误:

C:\glassfish5\java_ee_sdk-8\glassfish5\docs\javaee-tutorial\examples\jms\simple\
producer>appclient -client target/producer.jar queue 3

org.omg.CORBA.COMM_FAILURE: FINE: 00410008: Connection abort  vmcid: OMG  minor
code: 8 completed: Maybe
        at com.sun.proxy.$Proxy38.connectionAbort(Unknown Source)
        at com.sun.corba.ee.impl.transport.ConnectionImpl.doOptimizedReadStrateg
y(ConnectionImpl.java:1287)
        at com.sun.corba.ee.impl.transport.ConnectionImpl.doWork(ConnectionImpl.
java:845)
        at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.performW
ork(ThreadPoolImpl.java:497)
        at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.run(Thre
adPoolImpl.java:540)
Caused by: org.omg.CORBA.COMM_FAILURE: FINE: 00410011: IOException received when
 reading from connection SocketOrChannelConnectionImpl[ java.nio.channels.Socket
Channel[connected local=/127.0.0.1:12255 remote=localhost/127.0.0.1:3700] ESTABL
ISHED true true]  vmcid: OMG  minor code: 11  completed: No
        at com.sun.proxy.$Proxy38.ioexceptionWhenReadingConnection(Unknown Sourc
e)
        at com.sun.corba.ee.impl.transport.ConnectionImpl.nonBlockingRead(Connec
tionImpl.java:1465)
        at com.sun.corba.ee.impl.transport.ConnectionImpl.doOptimizedReadStrateg
y(ConnectionImpl.java:1241)
        ... 3 more
Caused by: java.io.IOException: End-of-stream
        at com.sun.corba.ee.impl.transport.ConnectionImpl.nonBlockingRead(Connec
tionImpl.java:1458)
        ... 4 more
com.sun.enterprise.container.common.spi.util.InjectionException: Exception attem
pting to inject Res-Ref-Env-Property: javaeetutorial.producer.Producer/connectio
nFactory@javax.jms.ConnectionFactory@ resolved as: jndi: java:comp/DefaultJMSCon
nectionFactory@res principal: null@mail: null
No Runtime properties
Database Vendor : null
Create Tables at Deploy : false
Delete Tables at Undeploy : false into class javaeetutorial.producer.Producer: L
ookup failed for 'java:comp/env/javaeetutorial.producer.Producer/connectionFacto
ry' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.namin
g.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise
.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDISt
ateFactoryImpl}
        at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._i
nject(InjectionManagerImpl.java:740)
        at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.in
ject(InjectionManagerImpl.java:507)
        at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.in
jectClass(InjectionManagerImpl.java:235)
        at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.in
jectClass(InjectionManagerImpl.java:227)
        at org.glassfish.appclient.client.acc.AppClientContainer$ClientMainClass
Setting.getClientMainClass(AppClientContainer.java:636)
        at org.glassfish.appclient.client.acc.AppClientContainer.getMainMethod(A
ppClientContainer.java:525)
        at org.glassfish.appclient.client.acc.AppClientContainer.completePrepara
tion(AppClientContainer.java:419)
        at org.glassfish.appclient.client.acc.AppClientContainer.prepare(AppClie
ntContainer.java:320)
        at org.glassfish.appclient.client.AppClientFacade.prepareACC(AppClientFa
cade.java:283)
        at org.glassfish.appclient.client.acc.agent.AppClientContainerAgent.prem
ain(AppClientContainerAgent.java:83)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(Instrumenta
tionImpl.java:386)
        at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(Instrument
ationImpl.java:401)
Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/javaee
tutorial.producer.Producer/connectionFactory' in SerialContext[myEnv={java.namin
g.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.
naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com
.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is jav
ax.naming.NamingException: Lookup failed for 'java:comp/DefaultJMSConnectionFact
ory' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.nami
ng.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterpris
e.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIS
tateFactoryImpl, com.sun.enterprise.naming.logicalName=java:comp/env/javaeetutor
ial.producer.Producer/connectionFactory} [Root exception is javax.naming.NamingE
xception: Lookup failed for 'jms/__defaultConnectionFactory' in SerialContext[my
Env={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContex
tFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.fa
ctory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root e
xception is javax.naming.NamingException: Unable to acquire SerialContextProvide
r for SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming
.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.
naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDISta
teFactoryImpl} [Root exception is org.omg.CORBA.COMM_FAILURE: FINE: 00410008: Co
nnection abort  vmcid: OMG  minor code: 8 completed: Maybe]]]]
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.jav
a:491)
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.jav
a:438)
        at javax.naming.InitialContext.lookup(InitialContext.java:417)
        at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._i
nject(InjectionManagerImpl.java:638)
        ... 15 more
Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/DefaultJMS
ConnectionFactory' in SerialContext[myEnv={java.naming.factory.initial=com.sun.e
nterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com
.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presenta
tion.rmi.JNDIStateFactoryImpl, com.sun.enterprise.naming.logicalName=java:comp/e
nv/javaeetutorial.producer.Producer/connectionFactory} [Root exception is javax.
naming.NamingException: Lookup failed for 'jms/__defaultConnectionFactory' in Se
rialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.Se
rialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming,
java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactor
yImpl} [Root exception is javax.naming.NamingException: Unable to acquire Serial
ContextProvider for SerialContext[myEnv={java.naming.factory.initial=com.sun.ent
erprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.s
un.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentati
on.rmi.JNDIStateFactoryImpl} [Root exception is org.omg.CORBA.COMM_FAILURE: FINE
: 00410008: Connection abort  vmcid: OMG  minor code: 8 completed: Maybe]]]
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.jav
a:491)
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.jav
a:438)
        at javax.naming.InitialContext.lookup(InitialContext.java:417)
        at com.sun.enterprise.naming.util.JndiNamingObjectFactory.create(JndiNam
ingObjectFactory.java:90)
        at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(Glas
sfishNamingManagerImpl.java:745)
        at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(Glas
sfishNamingManagerImpl.java:715)
        at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.j
ava:159)
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.jav
a:471)
        ... 18 more
Caused by: javax.naming.NamingException: Lookup failed for 'jms/__defaultConnect
ionFactory' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterpri
se.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.en
terprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rm
i.JNDIStateFactoryImpl} [Root exception is javax.naming.NamingException: Unable
to acquire SerialContextProvider for SerialContext[myEnv={java.naming.factory.in
itial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.facto
ry.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.e
e.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is org.omg.CORBA.C
OMM_FAILURE: FINE: 00410008: Connection abort  vmcid: OMG  minor code: 8 complet
ed: Maybe]]
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.jav
a:491)
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.jav
a:438)
        at javax.naming.InitialContext.lookup(InitialContext.java:417)
        at com.sun.enterprise.connectors.jms.system.DefaultJMSConnectionFactory.
handle(DefaultJMSConnectionFactory.java:84)
        at com.sun.enterprise.naming.impl.NamedNamingObjectManager.tryNamedProxi
es(NamedNamingObjectManager.java:135)
        at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.j
ava:164)
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.jav
a:471)
        ... 25 more
Caused by: javax.naming.NamingException: Unable to acquire SerialContextProvider
 for SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.
impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.n
aming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStat
eFactoryImpl} [Root exception is org.omg.CORBA.COMM_FAILURE: FINE: 00410008: Con
nection abort  vmcid: OMG  minor code: 8 completed: Maybe]
        at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContex
t.java:334)
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.jav
a:477)
        ... 31 more
Caused by: org.omg.CORBA.COMM_FAILURE: FINE: 00410008: Connection abort  vmcid:
OMG  minor code: 8 completed: Maybe
        at com.sun.proxy.$Proxy38.connectionAbort(Unknown Source)
        at com.sun.corba.ee.impl.transport.ConnectionImpl.doOptimizedReadStrateg
y(ConnectionImpl.java:1287)
        at com.sun.corba.ee.impl.transport.ConnectionImpl.doWork(ConnectionImpl.
java:845)
        at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.performW
ork(ThreadPoolImpl.java:497)
        at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.run(Thre
adPoolImpl.java:540)
Caused by: org.omg.CORBA.COMM_FAILURE: FINE: 00410011: IOException received when
 reading from connection SocketOrChannelConnectionImpl[ java.nio.channels.Socket
Channel[connected local=/127.0.0.1:12255 remote=localhost/127.0.0.1:3700] ESTABL
ISHED true true]  vmcid: OMG  minor code: 11  completed: No
        at com.sun.proxy.$Proxy38.ioexceptionWhenReadingConnection(Unknown Sourc
e)
        at com.sun.corba.ee.impl.transport.ConnectionImpl.nonBlockingRead(Connec
tionImpl.java:1465)
        at com.sun.corba.ee.impl.transport.ConnectionImpl.doOptimizedReadStrateg
y(ConnectionImpl.java:1241)
        ... 3 more
Caused by: java.io.IOException: End-of-stream
        at com.sun.corba.ee.impl.transport.ConnectionImpl.nonBlockingRead(Connec
tionImpl.java:1458)
        ... 4 more

C:\glassfish5\java_ee_sdk-8\glassfish5\docs\javaee-tutorial\examples\jms\simple\
producer>

我知道Glassfish服务器正在运行,因为我可以登录管理控制台。我注意到部分异常错误消息提到了数据库。消息的这一部分如下,“com.sun.enterprise.container.common.spi.util.InjectionException:尝试注入Res-Ref-Env-Property的异常:javaeetutorial.producer.Producer/connectionFactory@javax.jms。 ConnectionFactory @解析为:jndi:java:comp / DefaultJMSCon nectionFactory @ res principal:null @ mail:null 没有运行时属性 数据库供应商:null 在部署时创建表:false 在取消部署时删除表“

我尝试启动Glassfish服务器中的数据库Derby但收到错误。我认为这与端口号有关。我需要在特定的端口号上启动数据库......也许...... 0_O

有谁知道出了什么问题?我正在尝试学习JMS,Java EE的东西。我只是按照指示。我不知道出了什么问题。我认为主要问题是这些类型的例外:

com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Res-Ref-Env-Property: javaeetutorial.producer.Producer/connectionFactory@javax.jms.ConnectionFactory@ resolved as: jndi: java:comp/DefaultJMSConnectionFactory@res principal: null@mail: null

我有一种感觉,我永远不会知道答案。 :0

更新 - 该示例现在(对我而言)

嗯,我不知道导致问题的是什么,但我能够得到运行的例子。我所做的是再次通过示例并mvn clean install重新构建Producer.java程序。这种类型的Java EE应用程序中有很多“移动部件”。我不知道出了什么问题。 0_o下面的屏幕截图显示该示例在没有所有先前错误的情况下运行,如本文顶部的网页所示。 enter image description here

0 个答案:

没有答案