@Reference used services are not Injecting in Liferay 7.4GA93 Upgraded Code

David H Nebinger, modified 1 Year ago. New Member Posts: 12 Join Date: 4/18/24 Recent Posts

Hi Team,

We have recently upgraded our liferay maven project from 7.0 version to 7.4GA93 and we have updated the dependency in pom.xml file, but still our custom services are not injecting in web module. We are using @Component and @Reference annotation for injecting services.

We created one interface and implemented the interface with @Component annotation. Now we are trying to call the service by using @Reference in MVCrender method and it's failing. I have added my pom.xml, bnd and java class.

pom.xml:

<dependency>
<groupId>biz.aQute.bnd</groupId>
<artifactId>biz.aQute.bndlib</artifactId>
<version>${biz.aQute.bndlib.version}</version>
</dependency>
<dependency>
<groupId>com.liferay</groupId>
<artifactId>com.liferay.ant.bnd</artifactId>
<version>${com.liferay.ant.bnd.version}</version>
</dependency>
<dependency>
<groupId>javax.portlet</groupId>
<artifactId>portlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.core</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.versioning</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.http.servlet-api</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>com.liferay.portal.kernel</artifactId>
<version>4.13.1</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>6.0.0</version>
</dependency>

bnd.bnd file in service:

Bundle-Name: XXX
Bundle-SymbolicName: XXX
Bundle-Version: 3.0.0
Export-Package: \
   xxx, \
   yyy

-sources: true

bnd.bnd file in web module:

Bundle-Name: XXX
Bundle-SymbolicName: XXX
Bundle-Version: 3.0.0
Import-Package: \
    *
-jsp: *.jsp,*.jspf
-plugin.bundle: com.liferay.ant.bnd.resource.bundle.ResourceBundleLoaderAnalyzerPlugin
-plugin.jsp: com.liferay.ant.bnd.jsp.JspAnalyzerPlugin
-plugin.sass: com.liferay.ant.bnd.sass.SassAnalyzerPlugin
-sass: *
-sources: true

Interface Class:

public interface XXXService{}

Implementation Class:

@Component(immediate = true, service = XXXService.class)
public class XXXServiceImpl implements XXXService {}

MVCRender Class:

 

@Component(immediate = true, property = { "javax.portlet.name=" + xxx,
    "mvc.command.name=/xxx" }, service = MVCRenderCommand.class)
public class XXXMVCRender implements MVCRenderCommand {

  @Reference
  private XXXService xxxService;
  @Reference(unbind = "-")
  public void setXXXService(XXXService xxxService) {
    this.xxxService = xxxService;
  }

}

We are trying to call service in one of our method in MVCrender class using the @Reference.

Issue:

After updating depedency in pom.xml also we are not able to inject the services in web module. Could you please help us to resolve the issue ? None of the function are working in MVCrender due to injection this services. I got below error in gogo shell when use the scr:info {ID}

- XXXService: xxx.XXXService UNSATISFIED 1..1 static
target=(*) scope=bundle

7.4
thumbnail
David H Nebinger, modified 1 Year ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts

You should only be using one form of @Reference.

Either:

@Reference(unbind = "-")
private XXXService xxxService;

or

private XXXService xxxService;

@Reference(unbind = "-")
public void setXXXService(XXXService xxxService) {
  this.xxxService = xxxService;
}

but not the mix you have now.

Since it says XXXService is UNSATISFIED, that implies that the implementation of your service is not started.

In the gogo shell, use lb | grep XXX (replace XXX with something from the bundle name) to get the bundle id, then scr:list bundleId to get the list of services and their IDs, and scr:info serviceId to get the info about the service implementation.

The scr:info will report on what is up with your implementation and why it might not be started.

David H Nebinger, modified 1 Year ago. New Member Posts: 12 Join Date: 4/18/24 Recent Posts

Thanks David for your quick reply.

As you mentioned, we tried using the below and deployed successfully, still it's showing "UNSATISFIED 1..1 static
 target=(*) scope=bundle
" in gogshell.

@Reference(unbind = "-")
private XXXService xxxService;

or

private XXXService xxxService;

@Reference(unbind = "-")
public void setXXXService(XXXService xxxService) {
  this.xxxService = xxxService;
}

If i get "UNSATISFIED 1..1 static target=(*) scope=bundle" error in gogshell, what could be the issue ? because i can see my service/api are deployed successfully and its showing "Active" status. Tried b {Bundle_Id} and diag {Bundle_Id} also, nothing is coming in gogshell.

Could please help me to get this resolved ?

thumbnail
David H Nebinger, modified 1 Year ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts

The bundle being Active does not mean the contained components are all active.

Use the scr:list and scr:info commands to check the status of components in the bundle.

David H Nebinger, modified 1 Year ago. New Member Posts: 12 Join Date: 4/18/24 Recent Posts

Thanks David for your valuable response.

We checked our service builder and it was not migrated properly, that's reason those service and modules are not working. Do you have any blog to refer liferay upgrade from Liferay 7.0 to 7.4 GA93 for liferay maven project ? If yes, please share to us, that will help us to resolve our issue.

thumbnail
David H Nebinger, modified 1 Year ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts

I do not, but I would offer the following:

Consider switching from Maven to Gradle. Liferay has stopped work supporting Maven and at some point Maven usage might not be possible. Liferay is focused solely on using and supporting Gradle, so switching to Gradle is going to keep you in line with where Liferay is going.

For general SB upgrades, I typically recommend creating a new pair of SB modules in 7.4, then copy in your service.xml file, rebuild services, then add your methods from your 7.0 Impl classes, and rebuild services one more time.

The reason for this, when Service Builder is rebuilding services, it will not rewrite files that exist that do not require a change. For example, if you have Foo as an entity, you get a BaseFooLocalServiceImpl, a FooLocalServiceImpl, a FooLocalService interface, and the FooLocalServiceUtil class. If you add a method to FooLocalServiceImpl, only FooLocalService and FooLocalServiceUtil get regenerated; BaseFooLocalServiceImpl doesn't need to get modified at all.

When you are looking at an upgrade, in this case 7.0 to 7.4, if all you do is change the target platform and then rebuild services, SB determines that none of the files need to be regenerated because nothing has changed. It doesn't see the version change as something requiring regenerating the classes. This leads to errors like the one that you have, so basically you have code for 7.0 that otherwise thinks it could be ready for 7.4 since you did the rebuild, but internally it would reference classes and implementations from 7.0 that really don't apply to 7.4.

The path I shared will force SB to regenerate all of the necessary files, will have the right results for 7.4, and should make your SB upgrades work correctly under 7.4.

thumbnail
Christoph Rabel, modified 1 Year ago. Liferay Legend Posts: 1555 Join Date: 9/24/09 Recent Posts

Service Builder can be quite the pain to upgrade. There's also some errorpaths where the modules just fail without any message. Everything seems to start up, lb shows everything started, diag shows nothing, but ds:unsatisfied shows that the modules are not available without any reason given.

That said, copying the old service.xml to the newly created SB modules isn't sufficient. I found that different code is generated based on the dtd referenced in the doctype of the xml. You can try to update the service.xml based on a freshly created dtd for the current Liferay version. Maybe this already helps.

But here's what I do:

Create a new SB module with the exact same names as the old one. Copy only the entities(!) from the old xml.

Then regenerate the service builder. Afterwards use a merge tool on the IMPL classes to merge the old service builder implementation code to the newly generated classes. Regenerate again, try to build it. Fix any issues in the code.
Note: Previously spring was used as the dependency injector, so it is possible that your IMPL code does not work anymore. There is a blog post by David somewhere describing that change and here's some documentation: https://help.liferay.com/hc/en-us/articles/360032624732-Migrating-a-Service-Builder-Module-from-Spring-DI-to-OSGi-DS ​​​​​​​

Now, edit the new service.properties file. Update the build number to the version from the old module and regenerate again. If you don't do this, you will get see errormessages like "Version 5 is higher than 17" or so in the logs, which is caused by the mismatch in build numbers, but quite confusing due to the weird wording.

hth

Jamie Sammons, modified 1 Year ago. New Member Posts: 12 Join Date: 4/18/24 Recent Posts

Thanks team for your valuable response.

We have created new service builder in Liferay 7.4-GA93 and re-generated the service. Now it's working fine!

Jamie Sammons, modified 1 Year ago. New Member Posts: 2 Join Date: 4/8/24 Recent Posts

Hi All,

I was referring your discussion for similar error from my end. I tried same things but still I can see service is not recognized. Here is more information on my analysis. 

I am trying to migrate a module ( liferay gradle project ) from DXP 7.0 to 7.4 (liferay-dxp-7.4.13.u71) where it has 2 portlets defined. When I run scr:info on my bundle all my components of the bundle are satisfied except one portlet. Below is when I run scr:list [bundle id]

com.fmcna.dc.portal.quicklinks.portlet.QuickLinksPortlet in bundle 1,703 (com.fmcna.dc.portal.quicklinks:1.0.0) enabled, 1 instance.
    Id: 11295, State:SATISFIED
com.fmcna.dc.portal.quicklinks.portlet.QuickLinksSearchResultAddToPagePortlet in bundle 1,703 (com.fmcna.dc.portal.quicklinks:1.0.0) enabled, 1 instance.
    Id: 11296, State:UNSATISFIED REFERENCE
com.fmcna.dc.portal.quicklinks.configuration.action.QuickLinksPortletConfigurationAction in bundle 1,703 (com.fmcna.dc.portal.quicklinks:1.0.0) enabled, 1 instance.
    Id: 11293, State:SATISFIED
com.fmcna.dc.portal.quicklinks.service.impl.QuickLinksServiceImpl in bundle 1,703 (com.fmcna.dc.portal.quicklinks:1.0.0) enabled, 1 instance.
    Id: 11297, State:SATISFIED
com.fmcna.dc.portal.quicklinks.dao.impl.QuickLinksDaoImpl in bundle 1,703 (com.fmcna.dc.portal.quicklinks:1.0.0) enabled, 1 instance.
    Id: 11294, State:SATISFIED

 

On scr:info 11296 (for unsatisfied componenet) to understand whats missing, I can see towards the end -

References: (total 1) - QuickLinksService: com.fmcna.dc.portal.quicklinks.service.QuickLinksService UNSATISFIED 1..1 static target=(*) scope=bundle

 

Component Description: com.fmcna.dc.portal.quicklinks.portlet.QuickLinksSearchResultAddToPagePortlet
====================================================================================================
Class:         com.fmcna.dc.portal.quicklinks.portlet.QuickLinksSearchResultAddToPagePortlet
Bundle:        1703 (com.fmcna.dc.portal.quicklinks:1.0.0)
Enabled:       true
Immediate:     true
Services:      [javax.portlet.Portlet]
Scope:         singleton
Config PID(s): [com.fmcna.dc.portal.quicklinks.portlet.QuickLinksSearchResultAddToPagePortlet], Policy: optional
Base Props:    (9 entries)
  com.liferay.portlet.css-class-wrapper<String> = quicklinks-portlet
  com.liferay.portlet.display-category<String> = category.hidden
  com.liferay.portlet.instanceable<String> = false
  javax.portlet.display-name<String> = QuickLinks Search Result Add to Page Portlet
  javax.portlet.init-param.template-path<String> = /
  javax.portlet.init-param.view-template<String> = /quicklink.jsp
  javax.portlet.name<String> = com_fmcna_dc_portal_quicklinks_portlet_QuickLinksSearchResultAddToPagePortlet
  javax.portlet.resource-bundle<String> = content.Language
  javax.portlet.security-role-ref<String> = power-user,user

Component Configuration Id: 11296
---------------------------------
State:        UNSATISFIED REFERENCE
Config Props: (11 entries)
  com.liferay.portlet.css-class-wrapper<String> = quicklinks-portlet
  com.liferay.portlet.display-category<String> = category.hidden
  com.liferay.portlet.instanceable<String> = false
  component.id<Long> = 11296
  component.name<String> = com.fmcna.dc.portal.quicklinks.portlet.QuickLinksSearchResultAddToPagePortlet
  javax.portlet.display-name<String> = QuickLinks Search Result Add to Page Portlet
  javax.portlet.init-param.template-path<String> = /
  javax.portlet.init-param.view-template<String> = /quicklink.jsp
  javax.portlet.name<String> = com_fmcna_dc_portal_quicklinks_portlet_QuickLinksSearchResultAddToPagePortlet
  javax.portlet.resource-bundle<String> = content.Language
  javax.portlet.security-role-ref<String> = power-user,user
References:   (total 1)
  - QuickLinksService: com.fmcna.dc.portal.quicklinks.service.QuickLinksService UNSATISFIED 1..1 static
    target=(*) scope=bundle

Verified the reference, QuickLinksSearchResultAddToPagePortlet is referring to this service in below format which I believe is right.

private QuickLinksService quickLinksService;

    @Reference(unbind="-")
    public void setQuickLinksService(QuickLinksService quickLinksService) {
        this.quickLinksService = quickLinksService;
    }

    

On checking build.gradle file i have this

dependencies {
    compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "101.0.0"
    compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0"
    compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
    compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
    compileOnly group: "jstl", name: "jstl", version: "1.2"
    compileOnly group: "org.osgi", name: "osgi.cmpn", version: "6.0.0"
    compile     group: "biz.aQute.bnd", name: "biz.aQute.bndlib", version: "3.1.0"
    compile     group: "org.mongodb", name:"mongodb-driver-sync", version:"3.8.0"
    compile     group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.1'
    compile     group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.9.1'
    compile     group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.1'
    compileOnly group: "com.liferay", name: "com.liferay.dynamic.data.mapping.api", version: "3.5.2"
    compileOnly group: "com.liferay", name: "com.liferay.petra.string", version:"5.3.4"
    compile project(":modules:dc-api")
    
}

And here is my bnd.bnd file

Bundle-Name: quicklinks-portlet
-metatype: *
Bundle-SymbolicName: com.fmcna.dc.portal.quicklinks
Bundle-Version: 1.0.0
Export-Package: \
                com.fmcna.dc.portal.quicklinks.constants, \
                com.fmcna.dc.portal.quicklinks.service 

Include-Resource:  @jackson-core-2.9.1.jar;resolution:=optional, \
                   @jackson-databind-2.9.1.jar;resolution:=optional, \
                   @jackson-annotations-2.9.1.jar;resolution:=optional
            
Import-Package: \
    !com.mongodb.*\

I couldnt understand what could possibly go wrong here. But there should be something! I referred your discussion, @Reference used services are not Injecting in Liferay 7.4GA93 Upgraded Code and @Reference annotation prohibits portlet rendering - Forums - Liferay , couldnt find solution but learnt alot of information.

Any advise is much appreciated. Thanks in advance.