是否可以将多个捆绑包部署到使用Hibernate Envers的ServiceMix?

时间:2018-04-20 19:30:00

标签: java apache-karaf hibernate-envers apache-servicemix

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

单独地,捆绑包工作和审计就好了。但是,如果您同时部署了这两个软件包,则会出现错误:

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]

我创建了两个简单的包,可以根据标准的managed-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>

我认为这是所有相关文件。可能比我需要的更多,但比抱歉更安全。

我们希望能够将捆绑包作为单独的解决方案提供,客户可以根据需要使用这些解决方案。像一个HL7包和一个WebService包等等。

如果只有一个捆绑包可以使用Envers,那么我们将不得不开发一个额外的捆绑包来执行数据库更新,但如果捆绑包可以是自包含的,那就更好了。

1 个答案:

答案 0 :(得分:1)

您正在使用

提供envers扩展程序
<bean id="integrator" class="org.hibernate.envers.event.spi.EnversIntegrator" />
<service ref="integrator" interface="org.hibernate.integrator.spi.Integrator" />

由于这是一个OSGi服务,它将被所有捆绑包接收。所以最后开始的捆绑包会看到并抱怨。尝试将它放在自己的包中,以确保它只注册一次。