One Star

[resolved] How to use osgi service from job

Hello,
how can I use an osgi service deployed in TESB from a Talend job?
Thanks, Viktor
5 REPLIES
Community Manager

Re: [resolved] How to use osgi service from job

Hi 
You can use tRestClient component to call the rest webservcice you have deployed in Runtime.
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] How to use osgi service from job

Hello Shong,
thank you for your answer. My osgi service is not a webservice. It is java interface and implementation, service published using blueprint.
I want to access this service from talend job, that means I need to somehow access this service (something like BundleContext.getServiceReference), use its api without including the jar in the exported job (require/include in manifest).
Thank you,
Viktor
Community Manager

Re: [resolved] How to use osgi service from job

Hi
Maybe you can hard code on tJava or create a routine to access the service, load external jar using tLibraryLoad if needed.
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] How to use osgi service from job

Use sample routine:
package routines;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
public class OsgiHelper {
private OsgiHelper() {
}
public static <T> T getService(Class<T> clazz, String fullJobName) throws InvalidSyntaxException {
Bundle bundle = FrameworkUtil.getBundle(clazz);
if (bundle != null) {
BundleContext context = bundle.getBundleContext();
ServiceReference[] serviceReferences =
context.getServiceReferences(clazz.getName(), "(&(name=" + fullJobName.substring(fullJobName.lastIndexOf('.') + 1) + ")(type=job))");
if (null != serviceReferences) {
return clazz.cast(context.getService(serviceReferences));
}
}
return null;
}
}

And following call in job
System.out.println(routines.OsgiHelper.getService(TalendJob.class, this.getClass().getName()));
In Studio output is null, at runtime like esb.demoserviceconsumer_0_1.DemoServiceConsumer@79ee5eaf
One Star

Re: [resolved] How to use osgi service from job

Hello Alex,
thank you, looks very promising.
Not sure how it will work when I need to load api library for class T (interface of service) instead of requiring it in manifest. Will post update.
Viktor