It looks good.
Can you create another simple bundle with blueprint to see if the
reference is correctly injected (I'm pretty sure it is) ? In that case,
it's something in your application.
Regards
JB
On 01/15/2014 01:47 PM, [email protected] wrote:
Yes. This is what I got.
[javax.sql.DataSource]
----------------------
osgi.jndi.service.name = jdbc/masterdb
osgi.service.blueprint.compname = dataSource
service.id = 501
Provided by :
Bundle 149
regards
Vinu
-----Original Message-----
From: Jean-Baptiste Onofré [mailto:[email protected]]
Sent: Wednesday, January 15, 2014 6:11 PM
To: [email protected]
Subject: Re: Using JDBC data source from another OSGi service
Are you able to see the datasource service using the ls command ?
Regards
JB
On 01/15/2014 01:35 PM, [email protected] wrote:
Hi,
Yes I tried without filter. Got the same result.
regrds
Vinu
-----Original Message-----
From: Jean-Baptiste Onofré [mailto:[email protected]]
Sent: Wednesday, January 15, 2014 5:53 PM
To: [email protected]
Subject: Re: Using JDBC data source from another OSGi service
Hi,
did you try to remove the filter ? If you have only one datasource it should
work. It's just to check if the problem is not the filter.
Regards
JB
On 01/15/2014 12:45 PM, [email protected] wrote:
Started working on an application development with Apache Karaf as
run time recently. Have a problem with respect to accessing a JDBC
data source deployed as OSGi service from another OSGi service, both
using blueprint containers. I am following the tutorial
https://github.com/cschneider/Karaf-Tutorial/tree/master/db/examplejd
b c using apache karaf 3.0.0 and postgres 9.1 as database
Created the data source as below and deployed in deploy folder.
<?xml version="1.0" encoding="UTF-8"?> <blueprint
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean id="dataSource" class="org.postgresql.ds.PGPoolingDataSource"
destroy-method="close">
<property name="serverName" value="localhost:5432/masterdb"/>
<property name="user" value="userid"/>
<property name="password" value="password"/>
<property name="dataSourceName" value=" masterdb "/>
<property name="initialConnections" value="2"/>
<property name="maxConnections" value="4" />
</bean>
<service interface="javax.sql.DataSource" ref="dataSource">
<service-properties>
<entry key="osgi.jndi.service.name" value="jdbc/masterdb "/>
</service-properties>
</service>
</blueprint>
When I checked the JNDI names using the JNDI enterprise services its showing as
below. I am able to connect and query the database using jdbc enterprise
features using the data source created.
karaf@root()> jndi:names
JNDI Name | Class Name
---------------------------------------------------------------------
-
------- osgi:servicejdbc/masterDb |
org.postgresql.ds.PGPoolingDataSource
osgi:service/jndi | org.apache.karaf.jndi.internal.JndiServiceImpl
karaf@root()> jdbc:datasources
Name | Product | Version | URL
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
jdbc/masterDb| PostgreSQL | 9.1.1 |
jdbc:postgresql://localhost:5432/masterDb/null?loginTimeout=0&socketTimeout=0&prepareThreshold=5&unknownLength=2147483647&loglevel=0&tcpkeepalive=false
I am trying use this data source from another service which is
invoked from an Apache Camel Route. Code is as below
public class SvcMain implements ProcessingUnit {
static Logger LOG = LoggerFactory.getLogger(SvcMain.class);
private DataSource masterDb;
public void setMasterDb(DataSource masterDb) {
this.masterDb = masterDb;
if(this.masterDb == null) {
LOG.info("masterDb is null.");
}
}
public void process(DataSet dataset) throws ProcessingUnitException {
LOG.info(dataset.getRecords().toString());
try {
if(this.masterDb != null) {
Connection c = this.masterDb.getConnection();
DatabaseMetaData dbMeta = c.getMetaData();
LOG.info("Connected using datasource " +
dbMeta.getDatabaseProductName() + ", URL " + dbMeta.getURL());
}
else {
LOG.info("Data source masterDb is null.");
}
} catch (SQLException e) {
LOG.info("Error getting database connection,
"+e.getMessage());
}
}
}
Blueprint descriptor for the service is as below.
<?xml version="1.0" encoding="UTF-8"?> <blueprint
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0">
<reference id="masterDb" interface="javax.sql.DataSource"
filter="(osgi.jndi.service.name=jdbc/masterDb)"/>
<bean id="SvcImpl" class="com.foo.SvcMain">
<property name="masterDb" ref="masterDb"/>
</bean>
<service id="Svc" interface="com.foo.ProcessingUnit" ref="SvcImpl">
<service-properties>
<entry key="pu.svc.name" value="Svc" />
<entry key="pu.svc.version" value="1.0.0" />
</service-properties>
</service>
</blueprint>
In the service implementation, data source is getting injected properly. That
is in the setMasterDb method masterDb is not null which is called when the
service is getting deployed. But when it comes to process method which called
from camel route, masterDb is null. Anything else I need to do other than the
above mentioned ?
regards
Vinu
The information contained in this electronic message and any attachments to
this message are intended for the exclusive use of the addressee(s) and may
contain proprietary, confidential or privileged information. If you are not the
intended recipient, you should not disseminate, distribute or copy this e-mail.
Please notify the sender immediately and destroy all copies of this message and
any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient should
check this email and any attachments for the presence of viruses. The company
accepts no liability for any damage caused by any virus transmitted by this
email.
www.wipro.com
--
Jean-Baptiste Onofré
[email protected]
http://blog.nanthrax.net
Talend - http://www.talend.com
The information contained in this electronic message and any attachments to
this message are intended for the exclusive use of the addressee(s) and may
contain proprietary, confidential or privileged information. If you are not the
intended recipient, you should not disseminate, distribute or copy this e-mail.
Please notify the sender immediately and destroy all copies of this message and
any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient should
check this email and any attachments for the presence of viruses. The company
accepts no liability for any damage caused by any virus transmitted by this
email.
www.wipro.com
--
Jean-Baptiste Onofré
[email protected]
http://blog.nanthrax.net
Talend - http://www.talend.com
The information contained in this electronic message and any attachments to
this message are intended for the exclusive use of the addressee(s) and may
contain proprietary, confidential or privileged information. If you are not the
intended recipient, you should not disseminate, distribute or copy this e-mail.
Please notify the sender immediately and destroy all copies of this message and
any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient should
check this email and any attachments for the presence of viruses. The company
accepts no liability for any damage caused by any virus transmitted by this
email.
www.wipro.com
--
Jean-Baptiste Onofré
[email protected]
http://blog.nanthrax.net
Talend - http://www.talend.com