java—无法从独立客户端调用ejb

4si2a6ki  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(225)

我正在使用EJB3.2和glassfish服务器进行个人项目。我成功地部署了一个ear,该ear包含一个简单的ejb模块,其中包含一个无状态ejb和几个实体类,但是当我尝试从独立客户端调用无状态ejb时,出现以下错误:
线程“main”javax.naming.namenotfoundexception中出现异常:java:global
我尝试了一些在论坛上找到的建议,但似乎没有任何效果。
这是项目文件结构
ejb模块pom.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>DataManager</artifactId>
        <groupId>eu.tristan-schweizer</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>eu.tristan-schweizer</groupId>
    <artifactId>DataManager-ejb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>ejb</packaging>
    <name>DataManager-ejb</name>
    <url>http://www.example.com</url> <!-- FIXME -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <ejbVersion>3.2</ejbVersion>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>3.1.1</version>
            </plugin>
        </plugins>
  </build>
</project>

ear pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>DataManager</artifactId>
        <groupId>eu.tristan-schweizer</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>eu.tristan-schweizer</groupId>
    <artifactId>DataManager-ear</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>ear</packaging>
    <name>DataManager-ear</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>${project.parent.artifactId}-ejb</artifactId>
            <version>${project.version}</version>
            <type>ejb</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.10.1</version>
                <configuration>
                    <version>7</version>
                    <modules>
                        <ejbModule>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>${project.parent.artifactId}-ejb</artifactId>
                        </ejbModule>
                    </modules>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

ear应用程序.xml:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_8.xsd" version="8">
    <display-name>DataManager</display-name>
    <module>
        <ejb>DataManager-ejb-1.0-SNAPSHOT.jar</ejb>
    </module>
</application>

ear 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>
    <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="org.apache.derby.jdbc.ClientDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="derby_net_datamanager_dmrootPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
        <property name="serverName" value="localhost"/>
        <property name="portNumber" value="1527"/>
        <property name="databaseName" value="datamanager"/>
        <property name="User" value="dmroot"/>
        <property name="Password" value="root"/>
        <property name="URL" value="jdbc:derby://localhost:1527/datamanager"/>
        <property name="driverClass" value="org.apache.derby.jdbc.ClientDriver"/>
    </jdbc-connection-pool>
    <jdbc-resource enabled="true" jndi-name="java:app/datamanager" object-type="user" pool-name="derby_net_datamanager_dmrootPool"/>
</resources>

如图所示,glassfish服务器上的一切似乎都正常。
现在,独立客户端如下所示:

/*
 * Copyright 2019 tristan-schweizer.eu.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.tristan_schweizer.datamanager.wdi;

import eu.tristan_schweizer.datamanager.ejb.session.PositorManager;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public final class Importer
{

    private Importer ()
    {
        super();
    }

    public static void main (final String[] args) throws NamingException
    {
        Context ctxt = new InitialContext();
        PositorManager pm = (PositorManager) ctxt.lookup("java:global/DataManager-ear-1.0-SNAPSHOT/DataManager-ejb-1.0-SNAPSHOT.jar/PositorManager");
        pm.register((short) 0);
    }

}

我尝试为lookup()函数使用不同的字符串值(例如添加“#”或“!”后跟类全名或仅使用类全名)。
我还将gf客户机工件添加到我的maven依赖项中(我尝试了许多版本)。目前,客户端的pom.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>eu.tristan-schweizer</groupId>
        <artifactId>DataManager</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>DataManager-wdi</artifactId>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>DataManager-ejb</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.common</groupId>
            <artifactId>glassfish-naming</artifactId>
            <version>10.0-b28</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.main.appclient</groupId>
            <artifactId>gf-client</artifactId>
            <version>6.0.0-RC2</version>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>
</project>

以下是在glassfish上部署ear时的日志:
gui部署:uploadtotempfile]]
uploadfilename=datamanager-ear-1.0-snapshot.ear]]===================availabilityenabled skipped]]访问未访问的引用]]
访问未访问的参考资料]]
eu.tristan_schweizer.datamanager.ejb.model.itemuriannex实际已转换]]
eu.tristan\u schweizer.datamanager.ejb.model.itemuriannexid实际已转换]]eu.tristan\u schweizer.datamanager.ejb.model.positor实际已转换]]
eu.tristan\u schweizer.datamanager.ejb.model.itemuriposit实际已转换]]eu.tristan\u schweizer.datamanager.ejb.model.metadata实际已转换]]
eu.tristan_schweizer.datamanager.ejb.model.item实际得到了转换]]eclipselink,版本:eclipse persistence services-2.7.4.v20190115-ad5b7c6b2a]]/file:/c:/program files/glassfish/glassfish5/glassfish/domains/domain1/applications/datamanager-ear-1.0-snapshot/datamanager-ejb-1.0-snapshot_jar//datamanager-pu login successful]]ejb positormanager的可移植jndi名称:[java:global/datamanager-ear-1.0-snapshot/datamanager-ejb-1.0-snapshot/positormanager, java:global/datamanager-ear-1.0-snapshot/datamanager-ejb-1.0-snapshot/positormanager!eu.tristan_schweizer.datamanager.ejb.session.positormanger]]]weld-000411:观察者方法[backedannotatedmethod]org.glassfish.sse.impl.serversenteventcdiextension.processannotatedtype(@observes processannotatedtype,beanmanager)接收所有注解类型的事件。考虑使用@withannotations或带边界的泛型类型来限制事件。]]weld-000411:观察者方法[backedannotatedmethod]public org.glassfish.jms.injection.jmscdiextension.processannotatedtype(@processannotatedtype)接收所有带注解类型的事件。考虑使用@withannotations或带边界的泛型类型来限制事件。]]weld-000411:观察者方法[backedannotatedmethod]public org.hibernate.validator.cdi.validationextension.processannotatedtype(@observes processannotatedtype)接收所有注解类型的事件。考虑使用@withannotations或带边界的泛型类型来限制事件。]]weld-000411:观察者方法[backedannotatedmethod]private org.glassfish.jersey.ext.cdi1x.internal.cdicomponentprovider.processannotatedtype(@processannotatedtype)接收所有注解类型的事件。请考虑使用@withannotations或带边界的泛型类型来限制事件。]]weld-000146:BeforeBeandDiscovery.addannotatedtype(annotatedtype)用于类com.sun.faces.flow.flowdiscoverycdihelper已从cdi 1.1中弃用!]]weld-000146:用于org.glassfish.jersey.ext.cdi1x.transaction.internal.webappexceptionholder类的beforebeandiscovery.addannotatedtype(annotatedtype)已从cdi 1.1中弃用!]]weld-000146:用于org.glassfish.jersey.ext.cdi1x.transaction.internal.webappexceptioninterceptor类的beforebeandiscovery.addannotatedtype(annotatedtype)已从cdi 1.1中弃用!]]weld-000146:用于org.glassfish.jersey.ext.cdi1x.transaction.internal.transactionalexceptionmapper类的beforebeandiscovery.addannotatedtype(annotatedtype)已从cdi 1.1中弃用。]]weld-000146:用于org.glassfish.jersey.ext.cdi1x.servlet.internal.cdiexternalrequestscope类的beforebeandiscovery.addannotatedtype(annotatedtype)已从cdi 1.1中弃用!]]weld-000146:用于com.ibm.jbatch.container.cdi.batchproducerbean类的beforebeandiscovery.addannotatedtype(annotatedtype)已从cdi 1.1中弃用!]]weld-000146:用于org.glassfish.cdi.transaction.transactionalinterceptormandatory类的beforebeandiscovery.addannotatedtype(annotatedtype)已从cdi 1.1中弃用!]]weld-000146:beforebeandiscovery.addannotatedtype(annotatedtype)用于org.glassfish.cdi.transaction.transactionalInterceptor类,cdi 1.1中不推荐使用!]]weld-000146:用于org.glassfish.cdi.transaction.transactionalinterceptornotsupported类的beforebeandiscovery.addannotatedtype(annotatedtype)已从cdi 1.1中弃用!]]weld-000146:用于org.glassfish.cdi.transaction.transactionalinterceptorrequired类的beforebeandiscovery.addannotatedtype(annotatedtype)已从cdi 1.1中弃用!]]weld-000146:用于org.glassfish.cdi.transaction.transactionalinterceptorrequiresnew类的beforebeandiscovery.addannotatedtype(annotatedtype)已从cdi 1.1中弃用!]]weld-000146:用于org.glassfish.cdi.transaction.transactionalinterceptorsupports类的beforebeandiscovery.addannotatedtype(annotatedtype)已从cdi 1.1中弃用!]]weld-000146:用于org.glassfish.jersey.ext.cdi1x.internal.cdicomponentprovider$jaxrsparamproducer类的beforebeandiscovery.addannotatedtype(annotatedtype<?>)已从cdi 1.1中弃用。]]datamanager-ear-1.0-snapshot在560毫秒内成功部署。]]删除上载到临时目录的文件]]
笔记:
没有ejb-jar.xml(我发现它对于ejb3.x是可选的)
没有@local或@remote接口(从ejb3.1开始,@stateless就足够了):我尝试添加它们,但没有结果
我省略了主项目pom.xml

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题