java - OSGi 与 JPA : Service not starting although code seems ok

标签 java jpa osgi

是的,我知道我应该使用 Declarative ServicesBlueprint,但是我试图找出一个使用较低级别 API 的简单示例来感受 OSGi。

我只是想知道代码出了什么问题以及为什么我根本无法启动服务!!??

我在这里使用了两种方法:一种使用 ServiceTracker,另一种使用 ServiceReference,我知道这两种方法都不可靠> 但有人可以帮我让这个示例代码正常工作吗?对此将非常感激!!

这是我的代码:

我有一个简单的帐户实体类:

package model.account;

import javax.persistence.*;

@Entity
public class Account {

@Id @GeneratedValue
int id;
double balance;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public double getBalance() {
    return balance;
}

public void setBalance(double balance) {
    this.balance = balance;
}

@Override
public String toString() {
    return "Account{" + "id=" + id + ", balance=" + balance + '}';
}

}

AccountClient 为:

package client;

public class AccountClient {

public void run(EntityManagerFactory emf) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();

    Account a = new Account();
    a.setBalance(100.0);
    em.persist(a);

    em.getTransaction().commit();

    TypedQuery<Account> q = em.createQuery("SELECT a FROM Account a", Account.class);
    List<Account> results = q.getResultList();
    System.out.println("\n*** Account Report ***");
    for (Account acct : results) {
        System.out.println("Account: " + acct);
    }
    em.close();
}
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
 <persistence-unit name="Accounts" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>model.account.Account</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.target-database" value="Derby"/>
        <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/accountDB;create=true"/>
        <property name="javax.persistence.jdbc.user" value="app"/>
        <property name="javax.persistence.jdbc.password" value="app"/>

        <property name="eclipselink.logging.level" value="FINE"/>
        <property name="eclipselink.logging.timestamp" value="false"/>
        <property name="eclipselink.logging.thread" value="false"/>
        <property name="eclipselink.logging.exceptions" value="true"/>
        <property name="eclipselink.orm.throw.exceptions" value="true"/>
        <property name="eclipselink.jdbc.read-connections.min" value="1"/>
        <property name="eclipselink.jdbc.write-connections.min" value="1"/>                        
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
        <property name="eclipselink.weaving" value="true"/>

    </properties>

最后是Activator,其ServiceReference为:

package client;

public class Activator implements BundleActivator {

BundleContext ctx;
ServiceReference[] serviceReferences;
EntityManagerFactory emf;

public void start(BundleContext context) throws Exception {
    ctx = context;
    System.out.println("Gemini JPA Basic Sample started");

    try{
        serviceReferences = context.getServiceReferences(
                      EntityManagerFactory.class.getName(),
                      "(osgi.unit.name=Accounts)");
    }catch(Exception e){
        e.printStackTrace();
    }
    if(serviceReferences != null){
        emf = (EntityManagerFactory)context.getService(serviceReferences[0]);
    }
    if(emf != null){
        new AccountClient().run(emf);
    }
}

public void stop(BundleContext context) throws Exception {
    if(serviceReferences != null){
        context.ungetService(serviceReferences[0]);
    }
    System.out.println("Gemini JPA Basic Sample stopped");
}
}

ServiceTracker:

package client;
public class Activator implements BundleActivator, ServiceTrackerCustomizer {

BundleContext ctx;
ServiceTracker emfTracker;

public void start(BundleContext context) throws Exception {
    ctx = context;
    System.out.println("Gemini JPA Basic Sample started");

    /* We are in the same bundle as the persistence unit so the services should be 
     * available when we start up (if nothing bad happened) and the tracker is really 
     * just saving us the lookup, but this is the idea of how you would listen for a 
     * persistence unit coming from another bundle.
     */
    emfTracker = new ServiceTracker(ctx, EntityManagerFactory.class.getName(), this);
    emfTracker.open();
    System.out.println("Started finally!!");
}

public void stop(BundleContext context) throws Exception {
    emfTracker.close();
    System.out.println("Gemini JPA Basic Sample stopped");
}

/*========================*/
/* ServiceTracker methods */
/*========================*/

public Object addingService(ServiceReference ref) {
    System.out.println("reached in add");
    Bundle b = ref.getBundle();
    System.out.println("Got ref");
    Object service = b.getBundleContext().getService(ref);
    System.out.println("service");
    String unitName = (String)ref.getProperty(EntityManagerFactoryBuilder.JPA_UNIT_NAME);
    System.out.println("search");

    if (unitName.equals("Accounts")) {
        new AccountClient().run((EntityManagerFactory)service);
        System.out.println("Found and started");
    }
    return service;
}
public void modifiedService(ServiceReference ref, Object service) {}
public void removedService(ServiceReference ref, Object service) {}    
}

最佳答案

您的 list 是否包含元持久性 header 以及 JPA Documentation 中所述的所有依赖项? 所有 bundle 都启动了吗?

也许这有帮助Installing and Starting Gemini JPA Applications

关于java - OSGi 与 JPA : Service not starting although code seems ok,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20560589/

相关文章:

javascript - 基本身份验证是正确的选择吗?

hibernate - 使用 JPA 中的自联接从特定行组中检索行号

java - JPA 中的缓存(Eclipselink + Dropwizard + MySQL)

java - 添加 Equinox OSGI 钩子(Hook)抛出 ClassNotFoundException

java - OSGi 包中访问标准 java 类时出现 NoClassDefFoundError

java - maven-bundle-plugin 失败并显示 "Invalid class file module-info.class"

java - 在屏幕上绘图,在shell外使用java swt?

java - 使用 HashMap 实现对 DFA 和 NFA 进行建模

java - REST - 如何处理不必要的请求?

spring-boot - 如何使用Spring data JPA与数据库中的非实体表建立外键关系?