无法将具有EJB的JBoss工作项目部署在Tomcat Plume上

时间:2018-08-26 16:57:29

标签: maven jpa java-ee jboss tomee-7

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

任务:从servlet获取用户数据,并通过JPA +休眠将其持久化到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: ...\jpaTomcat\ear\target\ear-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页面仅包含用于输入昵称,电子邮件,密码以及对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上使用.xml数据源这一事实外,完全相同的项目在JBoss服务器上运行良好,

<?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构建它会出现问题。

我花了很多时间尝试不同的东西。如有任何不清楚的地方,请随时问我问题。预先感谢!

1 个答案:

答案 0 :(得分:0)

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

如果您想在其他工件中使用RegisterEJB,则可以使用<packaging>jar</packaging>

这看起来也很奇怪:

@EJB
RegisterEJB registerEJB = new RegisterEJB();

应该是:

@EJB
RegisterEJB registerEJB;

@Inject也可以使用,并且可以将EJB更改为其他名称,并且仍然可以使用。