gpt4 book ai didi

java - 是否可以将多个 bundle 部署到使用 Hibernate Envers 的 ServiceMix?

转载 作者:行者123 更新时间:2023-12-02 11:20:39 24 4
gpt4 key购买 nike

我们正在尝试在 ServiceMix 6.1.4 中部署多个 bundle ,每个 bundle 都会对数据库进行更新,并且我们希望使用 Hibernate Envers 4.3.6.FINAL 审核这些更新。我们正处于概念验证阶段,因此我们仅构建 jar 文件并将它们放入 apache-servicemix-6.1.4 目录的部署目录中,每个文件都有一个单独的数据源蓝图文件。

单独来看,这些 bundle 的工作和审核都很好。但是,如果您部署了两个 bundle ,其中一个 bundle 将会失败并出现错误:

2018-04-20 14:00:10,635 | WARN  | mix-6.1.4/deploy | container                        | 260 - org.apache.aries.jpa.container - 1.0.2 | Error creating EntityManagerFactory
org.hibernate.event.service.spi.EventListenerRegistrationException: Duplicate event listener found
at org.hibernate.event.service.internal.EventListenerGroupImpl.listenerShouldGetAdded(EventListenerGroupImpl.java:143)[252:org.hibernate.core:4.3.6.Final]
at org.hibernate.event.service.internal.EventListenerGroupImpl.appendListener(EventListenerGroupImpl.java:108)[252:org.hibernate.core:4.3.6.Final]
at org.hibernate.event.service.internal.EventListenerGroupImpl.appendListeners(EventListenerGroupImpl.java:102)[252:org.hibernate.core:4.3.6.Final]
at org.hibernate.event.service.internal.EventListenerRegistryImpl.appendListeners(EventListenerRegistryImpl.java:172)[252:org.hibernate.core:4.3.6.Final]
at org.hibernate.envers.event.spi.EnversIntegrator.integrate(EnversIntegrator.java:84)[254:org.hibernate.envers:4.3.6.Final]
at Proxy7c321f84_ab1f_4b15_a78a_66080ada210e.integrate(Unknown Source)[:]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:312)[252:org.hibernate.core:4.3.6.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)[252:org.hibernate.core:4.3.6.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)[251:org.hibernate.entitymanager:4.3.6.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)[251:org.hibernate.entitymanager:4.3.6.Final]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)[252:org.hibernate.core:4.3.6.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)[251:org.hibernate.entitymanager:4.3.6.Final]
at org.hibernate.osgi.OsgiPersistenceProvider.createContainerEntityManagerFactory(OsgiPersistenceProvider.java:112)[253:org.hibernate.osgi:4.3.6.Final]
at org.apache.aries.jpa.container.impl.EntityManagerFactoryManager.createEntityManagerFactories(EntityManagerFactoryManager.java:336)[260:org.apache.aries.jpa.container:1.0.2]
at org.apache.aries.jpa.container.impl.EntityManagerFactoryManager.registerEntityManagerFactories(EntityManagerFactoryManager.java:239)[260:org.apache.aries.jpa.container:1.0.2]
at org.apache.aries.jpa.container.impl.EntityManagerFactoryManager.bundleStateChange(EntityManagerFactoryManager.java:182)[260:org.apache.aries.jpa.container:1.0.2]
at org.apache.aries.jpa.container.impl.PersistenceBundleManager.modifiedBundle(PersistenceBundleManager.java:301)[260:org.apache.aries.jpa.container:1.0.2]
at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.customizerModified(BundleHookBundleTracker.java:500)[9:org.apache.aries.util:1.1.1]
at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.customizerModified(BundleHookBundleTracker.java:433)[9:org.apache.aries.util:1.1.1]
at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$AbstractTracked.track(BundleHookBundleTracker.java:725)[9:org.apache.aries.util:1.1.1]
at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.bundleChanged(BundleHookBundleTracker.java:463)[9:org.apache.aries.util:1.1.1]
at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$BundleEventHook.event(BundleHookBundleTracker.java:422)[9:org.apache.aries.util:1.1.1]
at org.apache.felix.framework.util.SecureAction.invokeBundleEventHook(SecureAction.java:1103)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.util.EventDispatcher.createWhitelistFromHooks(EventDispatcher.java:695)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.util.EventDispatcher.fireBundleEvent(EventDispatcher.java:483)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.Felix.fireBundleEvent(Felix.java:4403)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.Felix.startBundle(Felix.java:2092)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundle(DirectoryWatcher.java:1243)[7:org.apache.felix.fileinstall:3.5.2]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundles(DirectoryWatcher.java:1215)[7:org.apache.felix.fileinstall:3.5.2]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.doProcess(DirectoryWatcher.java:506)[7:org.apache.felix.fileinstall:3.5.2]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:355)[7:org.apache.felix.fileinstall:3.5.2]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:307)[7:org.apache.felix.fileinstall:3.5.2]

我创建了两个简单的包,可以根据标准的托管 jpa 示例代码重现该问题。

第一个pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>org.hibernate.osgi</groupId>
<artifactId>managed-jpa-test1</artifactId>
<version>1.0.0</version>
<packaging>bundle</packaging>

<dependencies>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.enterprise</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.karaf.shell</groupId>
<artifactId>org.apache.karaf.shell.console</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>4.3.6.Final</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>org.hibernate.osgi.managed-jpa-test1</Bundle-SymbolicName>
<Bundle-Name>managed-jpa-test1</Bundle-Name>
<Bundle-Version>1.0.0</Bundle-Version>
<Private-Package>
org.hibernate.osgitest,
org.hibernate.osgitest.entity
</Private-Package>
<Import-Package>
org.hibernate.jpa,
org.apache.felix.service.command,
org.apache.felix.gogo.commands,
org.apache.karaf.shell.console;resolution:=optional;version="[3.0,4)",
org.apache.karaf.shell.commands;resolution:=optional;version="[3.0,4)",
javax.persistence;version="[1.0.0,2.1.0]",
<!-- Needed for proxying's Javassist enhancement during runtime -->
org.hibernate.proxy,
javassist.util.proxy,
*
</Import-Package>
<Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>

第二个pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>org.hibernate.osgi</groupId>
<artifactId>managed-jpa-test2</artifactId>
<version>1.0.0</version>
<packaging>bundle</packaging>

<dependencies>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.enterprise</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.karaf.shell</groupId>
<artifactId>org.apache.karaf.shell.console</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>4.3.6.Final</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>org.hibernate.osgi.managed-jpa-test2</Bundle-SymbolicName>
<Bundle-Name>managed-jpa-test2</Bundle-Name>
<Bundle-Version>1.0.0</Bundle-Version>
<Private-Package>
org.hibernate.osgitest2,
org.hibernate.osgitest2.entity
</Private-Package>
<Import-Package>
org.hibernate.jpa,
org.apache.felix.service.command,
org.apache.felix.gogo.commands,
org.apache.karaf.shell.console;resolution:=optional;version="[3.0,4)",
org.apache.karaf.shell.commands;resolution:=optional;version="[3.0,4)",
javax.persistence;version="[1.0.0,2.1.0]",
<!-- Needed for proxying's Javassist enhancement during runtime -->
org.hibernate.proxy,
javassist.util.proxy,
*
</Import-Package>
<Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>

第一个blueprint.xml:

<blueprint default-activation="eager"
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.0.0"
xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0">

<bean id="dpService" class="org.hibernate.osgitest.DataPointServiceImpl">
<jpa:context unitname="managed-jpa-test1" property="entityManager"/>
<tx:transaction method="*" value="Required"/>
</bean>
<service ref="dpService" interface="org.hibernate.osgitest.DataPointService"/>

<bean id="integrator" class="org.hibernate.envers.event.spi.EnversIntegrator" />
<service ref="integrator" interface="org.hibernate.integrator.spi.Integrator" />

<command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
<command>
<action class="org.hibernate.osgitest.command.AddCommand">
<property name="dpService" ref="dpService"/>
</action>
</command>
</command-bundle>
</blueprint>

第二个blueprint.xml:

<blueprint default-activation="eager"
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.0.0"
xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0">

<bean id="dpService2" class="org.hibernate.osgitest2.DataPointService2Impl">
<jpa:context unitname="managed-jpa-test2" property="entityManager2"/>
<tx:transaction method="*" value="Required"/>
</bean>
<service ref="dpService2" interface="org.hibernate.osgitest2.DataPointService2"/>

<bean id="integrator" class="org.hibernate.envers.event.spi.EnversIntegrator" />
<service ref="integrator" interface="org.hibernate.integrator.spi.Integrator" />

<command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
<command>
<action class="org.hibernate.osgitest2.command.AddCommand2">
<property name="dpService2" ref="dpService2"/>
</action>
</command>
</command-bundle>
</blueprint>

DataPoint.java:

@Entity
@Audited
public class DataPoint {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;

private String name;

public long getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

DataPoint2.java:

@Entity
@Audited
public class DataPoint2 {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;

private String name;

public long getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

第一个 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.0">
<persistence-unit name="managed-jpa-test1" transaction-type="JTA">
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=test1/mssql)</jta-data-source>

<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.archive.autodetection" value="class"/>
</properties>
</persistence-unit>
</persistence>

第二个 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.0">
<persistence-unit name="managed-jpa-test2" transaction-type="JTA">
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=test2/mssql)</jta-data-source>

<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.archive.autodetection" value="class"/>
</properties>
</persistence-unit>
</persistence>

DataPointService.java:

public interface DataPointService {

public void add(DataPoint dp);

}

DataPointServiceImpl.java:

public class DataPointServiceImpl implements DataPointService {

private EntityManager entityManager;

public void add(DataPoint dp) {
entityManager.persist( dp );
entityManager.flush();
}

public EntityManager getEntityManager() {
return entityManager;
}

public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}

}

AddCommand.java:

@Command(scope = "dp1", name = "add")
public class AddCommand implements Action {
@Argument(index=0, name="Name", required=true, description="Name", multiValued=false)
String name;

private DataPointService dpService;

public void setDpService(DataPointService dpService) {
this.dpService = dpService;
}

public Object execute(CommandSession session) throws Exception {
DataPoint dp = new DataPoint();
dp.setName( name );
dpService.add( dp );
return null;
}

}

DataPointService2.java:

public interface DataPointService2 {

public void add(DataPoint2 dp);

}

DataPointService2Impl.java:

public class DataPointService2Impl implements DataPointService2 {

private EntityManager entityManager2;

public void add(DataPoint2 dp) {
entityManager2.persist( dp );
entityManager2.flush();
}

public EntityManager getEntityManager2() {
return entityManager2;
}

public void setEntityManager2(EntityManager entityManager2) {
this.entityManager2 = entityManager2;
}

}

AddCommand2.java:

@Command(scope = "dp2", name = "add")
public class AddCommand2 implements Action {
@Argument(index=0, name="Name", required=true, description="Name", multiValued=false)
String name;

private DataPointService2 dpService2;

public void setDpService2(DataPointService2 dpService2) {
this.dpService2 = dpService2;
}

public Object execute(CommandSession session) throws Exception {
DataPoint2 dp = new DataPoint2();
dp.setName( name );
dpService2.add( dp );
return null;
}

}

数据源-test1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

<bean id="test1DataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="url" value="jdbc:jtds:sqlserver://localhost:1433;databaseName=XXXX;tds=8.0;lastupdatecount=true;uselobs=false"/>
<property name="username" value="sa"/>
<property name="password" value="XXXXX"/>
</bean>

<service interface="javax.sql.DataSource" ref="test1DataSource">
<service-properties>
<entry key="osgi.jndi.service.name" value="test1/mssql"/>
</service-properties>
</service>
</blueprint>

数据源-test2.xml

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

<bean id="test2DataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="url" value="jdbc:jtds:sqlserver://localhost:1433;databaseName=XXXX;tds=8.0;lastupdatecount=true;uselobs=false"/>
<property name="username" value="sa"/>
<property name="password" value="XXXXX"/>
</bean>

<service interface="javax.sql.DataSource" ref="test2DataSource">
<service-properties>
<entry key="osgi.jndi.service.name" value="test2/mssql"/>
</service-properties>
</service>
</blueprint>

我认为这就是所有相关文件。可能超出了我的需要,但安全总比后悔好。

我们希望能够将 bundle 作为单独的解决方案提供,供客户根据自己的需求使用。就像一个 HL7 bundle 和一个 WebService bundle 等等。

如果只能有一个使用 Envers 的 bundle ,那么我们将不得不开发一个额外的 bundle 来执行数据库更新,但如果这些 bundle 可以是独立的,那就更好了。

最佳答案

您正在使用以下方式提供 envers 扩展

<bean id="integrator" class="org.hibernate.envers.event.spi.EnversIntegrator" />
<service ref="integrator" interface="org.hibernate.integrator.spi.Integrator" />

由于这是一项 OSGi 服务,因此所有 bundle 都会采用它。因此最后启动的包将会看到两者并提示。尝试将其放入自己的 bundle 中,以确保它仅注册一次。

关于java - 是否可以将多个 bundle 部署到使用 Hibernate Envers 的 ServiceMix?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49948545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com