Java 带JPA的OSGi:虽然代码似乎正常,但服务未启动
是的,我知道我应该使用Java 带JPA的OSGi:虽然代码似乎正常,但服务未启动,java,jpa,osgi,Java,Jpa,Osgi,是的,我知道我应该使用声明性服务或蓝图,但是我正试图找出一个简单的例子,使用较低级别的API来感受OSGi 我只想知道代码有什么问题,为什么我根本无法启动服务 我在这里使用了两种方法:一种是使用ServiceTracker,另一种是使用ServiceReference,我知道这两种方法都不可靠,但有人能帮我让这个示例代码正常工作吗。我会非常感激的 这是我的密码: 我有一个简单的账户实体类: package model.account; import javax.persistence.*;
声明性服务
或蓝图
,但是我正试图找出一个简单的例子,使用较低级别的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 + '}';
}
}
帐户客户端为:
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();
}
}
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) {}
}
您的清单是否包含元数据持久性标头和中描述的所有依赖项? 所有包裹都准备好了吗
也许这会有帮助是的,我也是这样做的。我尝试了另一种方法,它将实例化EntityManagerFactory的对象并调用Persistence.createEntityManagerFactory(“Accounts”);其中给出了“未找到命名帐户的持久性单元”的错误。您对此有任何输入吗?Persistence.createEntityManagerFactory(“Accounts”)进行类路径扫描,在OSGi环境中不起作用。Gemini JPA使用Extender模式定位Persistence.xml文件。因此,JPA扩展器将等待清单中包含元持久性头的包。如果找到一个,它将使用这个bundles上下文来查找这个bundle中的persistence.xml文件。然后Gemini JPA读取persistence.xml文件,并使用persistence bunlde上下文加载实体。然后将配置的EntityManagerFactory作为服务添加到上下文中。如果这不起作用,那么可能是您的持久性捆绑包或JPA extender没有启动,元持久性丢失了……我检查了MANIFEST.MF是否仅在构建项目时创建。但在执行maven clean时,它并不存在。是pom.xml正在生成清单吗?如果是,我如何更新它以使其包含元持久性?我通过确保所有依赖项都已启动但未创建EMF来尝试代码。服务不返回。现在我如何更新构建,使清单包含元持久性。请帮忙。非常感谢。
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");
}
}
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) {}
}