我一直在研究将JPA
与OSGi
一起使用的演示示例。
问题是,捆绑后我可以启动/停止服务,但是我无法获得serviceReference
。因此,我的JPA
实现未得到执行。
以下是代码:Activator.java
package manning.osgi.jpa;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
public class Activator implements BundleActivator {
public void start(BundleContext context) throws Exception {
LoginEvent loginEvent = new LoginEvent();
// Set login event...
loginEvent.setUserid("alex");
try {
ServiceReference [] serviceReferences =
context.getServiceReferences(
EntityManagerFactory.class.toString(),
"(osgi.unit.name=LoginEvent)");
System.out.println("Service References Created");
if (serviceReferences != null) {
EntityManagerFactory emf =
(EntityManagerFactory) context.getService(serviceReferences[0]);
EntityManager em = emf.createEntityManager();
persistLoginEvent(em, loginEvent);
System.out.println("Transaction started");
loginEvent = retrieveLoginEvent(em, loginEvent.getId());
System.out.println("Transaction completed");
em.close();
emf.close();
}
} catch (Exception e) {
System.out.println("Exception"+e.getMessage());
}
}
private void persistLoginEvent(EntityManager em, LoginEvent loginEvent) {
em.getTransaction().begin();
em.persist(loginEvent);
em.getTransaction().commit();
}
private LoginEvent retrieveLoginEvent(EntityManager em, int id) {
em.getTransaction().begin();
LoginEvent loginEvent = em.find(LoginEvent.class, id);
loginEvent.getUserid();
loginEvent.getId();
loginEvent.getTimestamp();
em.getTransaction().commit();
return loginEvent;
}
public void stop(BundleContext context) throws Exception {
}
}
LoginEvent.java
package manning.osgi.jpa;
import javax.persistence.*;
@Entity
public class LoginEvent {
@Id
@GeneratedValue
private int id;
private String userid;
private long timestamp;
public int getId() {
System.out.println("\nID: "+this.id);
return this.id;
}
public String getUserid() {
System.out.println("\nUser ID: "+this.userid);
return this.userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public long getTimestamp() {
System.out.println("\nTime Stamp: "+this.timestamp);
return this.timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
}
persistence.xml
<persistence version="1.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_1_0.xsd">
<persistence-unit name="LoginEvent">
<class>manning.osgi.jpa.LoginEvent</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.url"
value="jdbc:derby:derbyDB;create=true"/>
</properties>
</persistence-unit>
</persistence>
MANIFEST.MF
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: OsgiDemo
Bundle-SymbolicName: manning.osgi.jpa
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: manning.osgi.jpa.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: javax.persistence,
org.osgi.framework;version="1.3.0"
Bundle-ActivationPolicy: lazy
Require-Bundle: javax.persistence;bundle-version="2.1.0"
代码正在编译,但是由于
serviceReference
为null,所以无法继续。我重新检查了persistence-unit
名称及其正确性。谁能帮助我解决我在这里缺少的东西。
谢谢。
最佳答案
在您的激活器中,您不能确定所需的ServiceReference
已可用。您的捆绑包可能在注册该服务的捆绑包之前启动。另一个捆绑包也有可能无法启动,因此该服务将不会被注册。
如果要编写大量代码,可以在start函数中创建一个ServiceTracker
,并在跟踪器的addingService
函数中执行与在BundleActivator
的start函数中相同的操作。
如果您想减少工作量,请使用声明式服务(网上有许多教程)。在那里,您可以定义在您需要的OSGi服务可用之前,组件不应启动。
顺便说一句:如果使用了unGetService
函数,则应调用getService(serviceReference)
。
关于java - 具有OSGi的JPA示例在serviceReference中返回null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20144530/