使用EJB的JBoss工作项目无法部署在Tomcat Plume上

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用EJB的JBoss工作项目无法部署在Tomcat Plume上相关的知识,希望对你有一定的参考价值。

项目结构:pom-root,耳朵项目,依赖于ejb和战争。战争项目也取决于ejb。一切都是由mvn archetype:generate创建并在IDEA中管理的。整个项目得到清理和打包,然后在TomcatEE Plume 7.0.5上部署耳朵。

任务:从servlet获取用户数据并使用JPA + hibernate将其保存到PostgreSQL。

实体类:NewUser

无状态类:UserEJB

Maven清理和安装顺利,但是当我使用Tomcat启动它时,以下错误会阻止部署ear:

    26-Aug-2018 18:24:37.684 INFO [http-nio-8002-exec-5] org.apache.openejb.assembler.classic.ReloadableEntityManagerFactory.createDelegate PersistenceUnit(name=userUnit, provider=org.hibernate.jpa.HibernatePersistenceProvider) - provider time 282ms
    26-Aug-2018 18:24:37.684 INFO [http-nio-8002-exec-5] org.apache.openejb.assembler.classic.Assembler.destroyApplication Undeploying app: ...jpaTomcatear	argetear-1.0-SNAPSHOT
    26-Aug-2018 18:24:38.173 SEVERE [http-nio-8002-exec-5] org.apache.openejb.assembler.classic.Assembler.destroyApplication undeployException original cause
     java.lang.Exception: deployment not found: RegisterEJB

NewUser类:

package com.jeorgius.entities;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "newuser", schema = "userdata")
@SequenceGenerator(name = "h", sequenceName = "userdata.hibernate_sequence")
public class NewUser implements Serializable {

    @Id 
    @GeneratedValue (strategy = GenerationType.SEQUENCE, generator = "h")
    private Integer id;
    private String nick;
    private String email;
    private String pw;

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPw() {
        return pw;
    }

    public void setPw(String pw) {
        this.pw = pw;
    }
}

RegisterEJB类:

package com.jeorgius.ejb;

import com.jeorgius.entities.NewUser;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class RegisterEJB {
    @PersistenceContext(unitName = "userUnit")
    EntityManager em;

    public void createUser(String nick, String email, String pw) {
        NewUser newUser = new NewUser();
        newUser.setNick(nick);
        newUser.setEmail(email);
        newUser.setPw(pw);

        em.persist(newUser);
    }
}

index.jsp页面只包含用于输入nick,email,pw以及对servlet的操作的表单注册:

package com.jeorgius.servlets;

import com.jeorgius.ejb.RegisterEJB;

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("/register")
public class Register extends HttpServlet {

    @EJB
    RegisterEJB registerEJB = new RegisterEJB();

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String nick = req.getParameter("nick");
        String email = req.getParameter("email");
        String pw = req.getParameter("pw");

        registerEJB.createUser(nick, email, pw);

    }
}

ejb-project上的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="userUnit" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>com.jeorgius.entities.NewUser</class>
        <properties>

            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/jeorgius" />
            <property name="javax.persistence.jdbc.user" value="postgres" />
            <property name="javax.persistence.jdbc.password" value="1234" />

            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="false" />

        </properties>
    </persistence-unit>

</persistence>

ejb-project 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>jpaTomcat</artifactId>
    <groupId>com.jeorgius</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

    <artifactId>back</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>ejb</packaging>

    <name>back</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/postgresql/postgresql -->
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901-1.jdbc4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.3.5.Final</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>jpaTomcat</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <ejbVersion>3.1</ejbVersion>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

war-project 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>jpaTomcat</artifactId>
    <groupId>com.jeorgius</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

    <artifactId>front</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>front</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
            <scope>provided</scope>
        </dependency>


        <dependency>
            <groupId>com.jeorgius</groupId>
            <artifactId>back</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>ejb</type>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

ear-project 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>jpaTomcat</artifactId>
    <groupId>com.jeorgius</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

    <artifactId>ear</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>ear</packaging>

    <name>ear</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.jeorgius</groupId>
            <artifactId>back</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>ejb</type>
        </dependency>
        <dependency>
            <groupId>com.jeorgius</groupId>
            <artifactId>front</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>war</type>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <version>6</version>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

完全相同的项目在JBoss服务器上运行良好,除了我在JBoss上使用.xml数据源这一事实,所以persistence.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="userUnit">
        <jta-data-source>java:jboss/jeorgiusDS</jta-data-source>
        <class>com.jeorgius.entities.Signup</class>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="false" />
        </properties>
    </persistence-unit>

</persistence>

我只想在TomcatEE上测试我的应用程序,在使用JBoss之前我失败了,但我仍然无法弄清楚出了什么问题。 TomcatEE和Tomcat Plume应该与EJB和JPA一起使用。

Tomcat Plume也用在youtube上的一个例子中,它运行良好,但制作视频的人手动添加项目库而没有Maven。我自己并没有尝试过这样做,但这会留下用maven构建它的问题。

我花了很多时间尝试不同的东西。如果有什么不清楚的话,请随时问我问题。提前致谢!

答案

我认为TomEE不喜欢那个<packaging>ejb</packaging>。如果你将RegisterEJB移动到你的servlet所在的同一项目/模块,那么我相信它会工作(没有耳朵,只有一个war文件)。

如果你想在不同的工件中使用RegisterEJB,我会使用<packaging>jar</packaging>

这看起来很奇怪:

@EJB
RegisterEJB registerEJB = new RegisterEJB();

它应该是:

@EJB
RegisterEJB registerEJB;

@Inject也可以工作,你可以将EJB改为其他东西,它仍然可以工作。

以上是关于使用EJB的JBoss工作项目无法部署在Tomcat Plume上的主要内容,如果未能解决你的问题,请参考以下文章

如何在 JBoss 5 中订购 EJB 和 JMS 队列配置的部署?

JBoss5:由于 java.util.zip.ZipException 无法部署:打开 zip 文件时出错

远程客户端访问部署在 JBOSS AS 中的 EJB 的 JNDI 循环(7.1.1 最终版)

我无法设置我的 jndi.properties 来访问 Jboss 5 上的远程 EJB

无法在 JBoss 6.4 上查找远程 EJB3

EJB 依赖于另一个 EJB?