java - JPA 与 OSGi 示例在 serviceReference 中返回 null

标签 java jpa persistence osgi

我一直在研究演示示例,其中 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 为空,我无法继续。我重新检查了 persistence-unit 名称及其正确性。

任何人都可以帮我解决我在这里缺少的东西吗? 谢谢。

最佳答案

在您的 Activator 中,您无法确定您需要的 ServiceReference 是否已经可用。您的 bundle 可能会在注册该服务的 bundle 之前启动。也有可能其他 bundle 无法启动,因此服务不会注册。

如果您想要编写大量代码,您可以在启动函数中创建一个 ServiceTracker ,并在跟踪器的 addingService 函数中执行与您在BundleActivator 的启动函数。

如果您想减少工作量,请使用声明式服务(网上有许多教程)。您可以在那里定义您的组件在您需要的 OSGi 服务可用之前不应启动。

顺便说一句:如果您使用 getService(serviceReference) 函数,则应该调用 unGetService

关于java - JPA 与 OSGi 示例在 serviceReference 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20144530/

相关文章:

Java:通过反射访问私有(private)字段(行为)

java - 带有 JPARepository (MySQL) 的 Spring Boot 应用程序无法启动

java - 无法将 'java.lang.String' 的值转换为所需类型 'myEnum'

java - JBOSS V/s apache tomcat

java - Java 中的矩阵运算

java - Hibernate - TypedQuery.getResultList() 返回相同对象的列表

jpa - 如何配置 JPA 以使用 JNDI?

ios - Swift - 使用 plist 的 tableview 在添加新数据后不会重新加载

java - 使用 ShrinkWrap 找不到 test-persistence.xml

java - 掷骰子游戏问题