1

我有一个微服务,它是一个调度程序。我通过 REST json 获得了一份计时工作。我遵循了一些关于如何添加 persistence.xml 的教程,但它似乎不起作用。我没有收到任何失败消息,但来自 EntityManager 的 NullpointerException。

它不会创建表等,所以我认为添加persistence.xml 也有问题?

连接数据源没有问题。我测试了它,它似乎工作正常。

调度程序主要:

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset;
import org.wildfly.swarm.Swarm;
import org.wildfly.swarm.container.Container;
import org.wildfly.swarm.datasources.DatasourceArchive;
import org.wildfly.swarm.jaxrs.JAXRSArchive;

public class SchedulerMain {

public static void main(String[] args) {

    try {
        Container container = new Container();
        container.start();
        container.deploy(Swarm.artifact("mysql:mysql-connector-java", "mysql"));

        deployApp(container);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static void deployApp(Container container) throws Exception {
    JAXRSArchive app = ShrinkWrap.create(JAXRSArchive.class);

    app.as(DatasourceArchive.class).dataSource("Scheduler", (ds) -> {
        ds.connectionUrl("jdbc:mysql://localhost:3306/scheduler");
        ds.driverName("mysql");
        ds.userName("root");
        ds.password("");
    });

    app.addAsWebInfResource(new ClassLoaderAsset("META-INF/persistence.xml", SchedulerMain.class.getClassLoader()),
            "classes/META-INF/persistence.xml");

    app.addAsLibrary(container.createDefaultDeployment());
    app.addAllDependencies();

    container.deploy(app);
}
}

调度REST资源:

@ApplicationScoped
@Path("/schedule")
public class ScheduleRESTResource {

    @Inject
    private SchedulerController schedulerController;

    @Inject
    private ScheduleBean scheduleBean;

    @POST
    @Path("/job")
    @Consumes(MediaType.APPLICATION_JSON)
    public void schedule(MySchedule schedule) throws IOException {
        List<MySchedule> schedules = scheduleBean.getAllSchedules();
        System.out.println(schedules.toString());
        schedulerController.buildSchedule(schedule);
    }
}

调度豆:

import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;

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

import de.fszm.scheduler.entities.MySchedule;

@Stateless
public class ScheduleBean {

    @PersistenceContext(unitName = "Scheduler")
    private EntityManager em;

    public List<MySchedule> getAllSchedules() {
        System.out.println("getAllSchedules");

        List<MySchedule> schedules = new ArrayList<>();

        if (em != null) {
            schedules = em.createNamedQuery("Schedule.getAllSchedules",     MySchedule.class).getResultList();
        } else {
            System.out.println("em is null");
        }

        return schedules;
    }
}

持久性.xml:

<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="Scheduler" transaction-type="JTA">

    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <jta-data-source>java:jboss/datasources/Scheduler</jta-data-source>

    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/scheduler"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.username" value="root"/>
        <property name="hibernate.connection.password" value=""/>
        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.hbm2ddl.auto" value="drop-and-create"/>
    </properties>
</persistence-unit>

pom.xml:

<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>
<groupId>...</groupId>
<artifactId>scheduler</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>scheduler</name>
<packaging>jar</packaging>

<properties>
    <version.swarm>1.0.0.Beta6</version.swarm>
    <version.mysql>5.1.6</version.mysql>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>bom</artifactId>
            <version>${version.swarm}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>undertow</artifactId>
    </dependency>

    <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>jaxrs</artifactId>
    </dependency>

    <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>ejb</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>

    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>jaxrs-weld</artifactId>
    </dependency>

    <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>datasources</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${version.mysql}</version>
    </dependency>

<!-- <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>wildfly-swarm-ejb</artifactId>
        <version>1.0.0.Alpha5</version>
    </dependency> -->

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>wildfly-swarm-plugin</artifactId>
            <version>${version.swarm}</version>
            <configuration>
            </configuration>
            <executions>
                <execution>
                    <id>package</id>
                    <goals>
                        <goal>package</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin> 
    </plugins>
</build>

4

0 回答 0