Foren

Liferay 7 application startup event not able to call ServiceBuilder method

thumbnail
Rajesh Rathod, geändert vor 7 Jahren.

Liferay 7 application startup event not able to call ServiceBuilder method

New Member Beiträge: 16 Beitrittsdatum: 20.03.12 Neueste Beiträge
Hi All,

I would like to invoke ServiceBuilder method (*LocalServiceUtil.getXXX()) in OSGI module which implement component for application.startup.events. Can anyone please guide how we can do that?

At present its throwing NullPointerException in LocalServiceUtil class because its not able to find services class (*LocalServiceImpl) in OSGI service registry. I think its right behavior because application.startup.events class get invoke when portal is starting and I believe other OSGI module (ServiceBuilder nature) may not have registered services in OSGI registry.

Is there any workaround to solve this issue?

Regards,
Rajesh Rathod
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: Liferay 7 application startup event not able to call ServiceBuilder met

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
Once again, if you have an OSGi module you should NEVER be using the XxxLocalServiceUtil classes. They are only for legacy war based portlets.

You should only be using @Reference annotations so OSGi injects the service instance.
thumbnail
Rajesh Rathod, geändert vor 7 Jahren.

RE: Liferay 7 application startup event not able to call ServiceBuilder met

New Member Beiträge: 16 Beitrittsdatum: 20.03.12 Neueste Beiträge
Thanks David for your quick response.

So does it mean that injecting services through @Reference annotation will ensure that target service is available before it gets inject in caller OSGI module?

I tried adding @Reference (Referencing to FooLocalService in osgi-moduleb-api) annotation in StartupAction class (osgi-modulea) but Its not invoking run method inside that class.

Please find below snippet of StartupAction class.

@Component(immediate = true, property = {"key=application.startup.events"}, service = LifecycleAction.class)
public class ABCStartupAction extends SimpleAction {

private FooLocalService fooLocalService;

@Override
public void run(String[] companyIds) throws ActionException {
System.out.println("**********************" + companyIds + " *************************");
System.out.println("********" + getFooLocalService().getName() + " ********************");
}

public FooLocalService getFooLocalService() {
return fooLocalService;
}

@Reference(unbind = "-")
public void setFooLocalService(FooLocalService fooLocalService) {
this.fooLocalService = fooLocalService;
}
}
thumbnail
Milen Dyankov, geändert vor 7 Jahren.

RE: Liferay 7 application startup event not able to call ServiceBuilder met

Expert Beiträge: 310 Beitrittsdatum: 30.10.12 Neueste Beiträge
I haven't checked in the code how this works exactly but my assumption is

First scenario (using FooLocalServiceUtil class) - your ABCStartupAction service does not depend on anything so it's registers with service registry quick enough. The startup action process finds it and call it. The FooLocalService is not registered yet so when you call the FooLocalServiceUtil you get null

Second scenario (using @Reference) - your ABCStartupAction service now depends on your FooLocalService service. By defaults @Reference is STATIC and MANDATORY which means your ABCStartupAction service will not be activated (and thus not registered) until the FooLocalService service is available. Which means at the time startup action process is fired you ABCStartupAction is not there so it's not called.

One thing you could try to do is make your ABCStartupAction to FooLocalService OPTIONAL and GREEDY. This way it will be registered even if FooLocalService is not available and updated when it becomes available. Then in your "run" method you'll have to either
- block and wait for FooLocalService to become available
- span a new thread and do the work asynchronously

Again, I haven't tested this and not even looked in the code. This is just from the top of my head based on my guess of what is happening.
thumbnail
Rajesh Rathod, geändert vor 7 Jahren.

RE: Liferay 7 application startup event not able to call ServiceBuilder met (Antwort)

New Member Beiträge: 16 Beitrittsdatum: 20.03.12 Neueste Beiträge
Thanks Milen. It's exactly the same what you have mentioned. I have also observed that this issue is with custom services only and if I try accessing Expando**Util.getXXX() then its working fine.

Can we conclude that application.startup.events class is not the right place to invoke any custom OSGI services?

I found that services which are exists in portal-impl.jar will always register first before it invokes any custom OSGI modules. Is that right? If yes then is it possible to register few of our custom OSGI services on same priority as portal-impl.jar?

Updating thread with latest findings
Its not able to find reference of LocalService class when I use @Reference in Activator class. I am using below code snippet and line with System.out.println is returning NULL to me.

public class ABCActivator implements BundleActivator {

private FooLocalService fooLocalService;

@Override
public void start(BundleContext bundleContext) throws Exception {
System.out.println(getFooLocalService());
}

@Override
public void stop(BundleContext bundleContext) throws Exception {
}

public FooLocalService getFooLocalService() {
return fooLocalService;
}

@Reference(unbind = "-")
public void setFooLocalService(FooLocalService fooLocalService) {
this.fooLocalService = fooLocalService;
}
}
thumbnail
Sunit Chatterjee, geändert vor 6 Jahren.

RE: Liferay 7 application startup event not able to call ServiceBuilder met

Junior Member Beiträge: 28 Beitrittsdatum: 18.05.17 Neueste Beiträge
Were you able to find a solution to this - Referencing custom osgi LocalServices in StartupAction/SimpleActino
I am also trying the same thing, and not working for me

Thanks
Sunit
thumbnail
David H Nebinger, geändert vor 6 Jahren.

RE: Liferay 7 application startup event not able to call ServiceBuilder met

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
You guys are missing the @Component reference.

OSGi will not bind in @Reference objects on things that are not @Components.