我正在尝试完成一个使用 RMI 将工作负载发送到一些远程服务器的原型。工作完成后,远程服务器应该将结果写回数据库。我使用 JPA 和 Hibernate 作为提供程序。但是我很难要求远程服务器这样做。
这是该部分的代码将通过 RMI 发送到远程服务器
public class UpdateDB implements ServerRun<Boolean>, Serializable {
private static final long serialVersionUID = 2683781993159094346L;
@Override
public Boolean execute() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "abc" );
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist( new User( "First user!", new Date() ) );
entityManager.persist( new User( "Second user", new Date() ) );
entityManager.getTransaction().commit();
entityManager.close();
entityManager.close();
entityManagerFactory.close();
return true;
}
}
这里是上面代码同级的resources/META-INF中的persistence.xml:</p>
<persistence 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"
version="2.0">
<persistence-unit name="abc" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>data.persist.User</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://192.168.205.103:3306/event_grp" />
<property name="javax.persistence.jdbc.user" value="username" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
如果我在服务器上运行 JPA,它就可以工作,而其他任何东西都可以在 RMI 功能方面工作。但是 EntityManagerFactory 不会启动,这是错误:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named abc
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at data.persist.UpdateDB.execute(UpdateDB.java:58)
at data.persist.UpdateDB.execute(UpdateDB.java:1)
at engine.ComputeEngine.executeTask(ComputeEngine.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:303)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at com.sun.proxy.$Proxy12.executeTask(Unknown Source)
at client.JobSubmitter.main(JobSubmitter.java:94)
因此,RMI 似乎无法在 META-INF 中找到或加载 persistence.xml。我对 log4j 也有类似的经验,但我通过在代码中定义所有内容而不是使用 log4j.properties 文件来解决问题。
编辑:要发送到远程服务器的代码包看起来像,这是我放置persistence.xml的唯一地方:
-src/main/java/data/persist/UpdataDB.java
|
src/main/resources/META-INF/persistence.xml
根据user1888440的问题,我把安全权限配置放在这里。客户端和远程服务器都使用以下权限:
grant {
permission java.security.AllPermission;
};
如何使远程服务器初始化 EntityManagerFactory 以便它可以写回数据库?如果您也可以提供一些简单的代码,我将不胜感激。