1

我有一个基本的 ESB Fuse 测试项目,其中包含以下模块:

简单数据源 简单模型 简单服务

数据源通过蓝图配置,数据源附加到jndi:

<?xml version="1.0" encoding="UTF-8"?>

http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd ">

<bean id="simpleDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@(DESCRIPTION=(LOAD_BALANCE=yes)(CONNECT_DATA=(SERVICE_NAME=xyz))(ADDRESS=(PROTOCOL=TCP)(HOST=xx.xx.xx.xx)(PORT=1521)))" />
    <property name="username" value="username" />
    <property name="password" value="password" />       
</bean>

<!--bean id="simpleDataSource" class="oracle.jdbc.driver.OracleDriver">     
    <property name="URL" value="jdbc:oracle:thin:@(DESCRIPTION=(LOAD_BALANCE=yes)(CONNECT_DATA=(SERVICE_NAME=devd))(ADDRESS=(PROTOCOL=TCP)(HOST=10.75.192.195)(PORT=1521)))"/>
    <property name="username" value="username" />
    <property name="password" value="password" />       
</bean-->

<service ref="simpleDataSource" interface="javax.sql.DataSource">
    <service-properties>
        <entry key="osgi.jndi.service.name" value="jdbc/simpleDataSource" />
    </service-properties>
</service>

该模型在 persistence.xml 文件中定义了一个持久单元,并通过 jndi 引用数据源(注意这里定义的长和短 jndi 查找,我都尝试过):

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="2.0">

<persistence-unit name="simple-service-persistence-unit" transaction-type="JTA">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <!--jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/simpleDataSource)</jta-data-source-->
    <jta-data-source>osgi.jndi.service.name=jdbc/simpleDataSource</jta-data-source>
    <!-- list of the persistance classes -->
    <class>com.model.SimpleRow</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>

SimpleRow 类使用 JPA 注释:

package com.model;

导入 javax.persistence.Column;导入 javax.persistence.Entity;导入 javax.persistence.Table;

@Entity @Table(name = "SIMPLE") 公共类 SimpleRow {

@Column(name = "simple_id")
private Long simpleId;

@Column(name = "simple_text", length =100)
private String simpleText;

public Long getSimpleId() {
    return simpleId;
}

public void setSimpleId(Long simpleId) {
    this.simpleId = simpleId;
}

public String getSimpleText() {
    return simpleText;
}

public void setSimpleText(String simpleText) {
    this.simpleText = simpleText;
}

}

然后我尝试将 EntityManager 注入服务,再次使用蓝图和对 simple-service-persistence-unit 的引用:

<?xml version="1.0" encoding="UTF-8"?>

http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://aries.apache.org/xmlns/ jpa/v1.1.0 http://aries.apache.org/schemas/jpa/jpa_110.xsd ">

<bean id="simpleService" class="com.service.SimpleServiceImpl">
    <jpa:context property="entityManager" unitname="simple-service-persistence-unit" />
    <tx:transaction method="*" value="Required" />
</bean>

<service ref="simpleService" interface="com.service.SimpleService" />

现在,当我将这些模块安装到 fuse OSGi 容器中时,simple-datasource 和 simple-module 似乎都安装正确。列出这些模块给出:

[ 274] [Active     ] [            ] [       ] [   60] Simple Model (1.0.0)
[ 275] [Active     ] [Created     ] [       ] [   60] Simple Datasource (1.0.0)

我创建了一个使用注入 DataSource 的测试 jdbc 模块,这证实了 DataSource 工作正常,例如

public class DbExample {
DataSource dataSource;

public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
}

public void test() throws Exception {
    Connection con = dataSource.getConnection();
    Statement stmt = null;
    DatabaseMetaData dbMeta = con.getMetaData();
    ....
 }

现在,当我尝试启动 simple-service 时,它​​会进入宽限期状态,并将以下消息打印到日志文件中:

2013-07-02 11:05:33,772 | INFO  | e-1.0.0-thread-1 | BlueprintContainerImpl           | ?                                   ? | 8 - org.apache.aries.blueprint.core - 1.0.1.fuse-71-047 | Bundle simple-service is waiting for dependencies [(&(&(org.apache.aries.jpa.proxy.factory=true)(osgi.unit.name=simple-service-persistence-unit))(objectClass=javax.persistence.EntityManagerFactory))]

列出模块状态表明它处于宽限期状态:

[ 277] [Active     ] [GracePeriod ] [       ] [   60] Simple Service Bundle (1.0.0)

它最终超时并进入故障状态。

现在我最初的想法是它可能是一个损坏的数据源,但 jdbc 模块证明它工作正常。我随后的想法是 jndi 查找工作不正常,尽管我不确定如何检查。有什么方法可以查看 jndi 注册表吗?欢迎任何其他建议。

4

1 回答 1

1

您的捆绑包正在等待服务出现,这可能是一个问题,因为您引用了一个不可用的服务(打开 DEBUG 登录,您将在日志中看到详细信息)或者有时会发生什么(取决于 Karaf/Aries 的底层版本),您需要切换蓝图以使用同步部署,因为有时等待的捆绑包不会捕获稍后启动的服务。为此,您需要翻转etc/config.properties文件中的org.apache.aries.blueprint.synchronous=true 。

于 2013-07-03T07:28:14.810 回答