Invoking Local Services

Once you deploy your services module, those services are available in the container. Service Builder generates local and remote service classes as OSGi Declarative Services (DS) components. These components are accessible to other DS components, so you can invoke them from other components, such as your web application. Here’s how:

  1. Add a reference to the local service component.

  2. Call the component’s methods.

There’s a Blade sample called Basic Service Builder. Its basic-web module has a Portlet service component that demonstrates referencing a local service component. This module also has JSPs that invoke the component’s methods. Your first step is to add a reference to the local service component object.

Step 1: Reference the Local Service Component

Your application’s Service Builder-generated local services are DS components that you can inject into your application’s other DS components (classes annotated with @Component) using the @Reference annotation. The basic-web module’s JSPPortlet class is a Portlet service component that references the FooLocalService local service as a DS component.

@Reference
private volatile FooLocalService _fooLocalService;

The OSGi service registry wires the service implementation object to your class that references it. The JSPPortlet sample class declares the _fooLocalService field to be volatile, but making a field volatile is completely optional.

Important: You should never invoke *LocalServiceImpl objects directly. You should only invoke them indirectly through their *LocalService service interface. The OSGi service registry wires the service implementation object to your class.

You can make a service object available to JSPs by associating it with a RenderRequest attribute. For example, the JSPPortlet’s render method associates the FooLocalService object with an attribute called fooLocalService.

@Override
public void render(RenderRequest request, RenderResponse response)
    throws IOException, PortletException {

    //set service bean
    request.setAttribute("fooLocalService", getFooLocalService());

    super.render(request, response);
}

public FooLocalService getFooLocalService() {
    return _fooLocalService;
}

If your JSP declares the <portlet:defineObjects /> tag, it can retrieve the service object from the RenderRequest attribute. For example, the JSPPortlet’s init.jsp file retrieves the FooLocalService object from the "fooLocalService" attribute.

...
<%@
page import="com.liferay.blade.samples.servicebuilder.service.FooLocalService" %>
...

<liferay-theme:defineObjects />

<portlet:defineObjects />

<%
...

//get service bean
FooLocalService fooLocalService = (FooLocalService)request.getAttribute("fooLocalService");
%>

All JSPs that include the above init.jsp can use the fooLocalService variable to invoke the local service component’s methods.

Step 2: Call the Component’s Methods

Now that you have the service component object, you can invoke its methods as you would any Java object’s methods.

The basic-web sample module’s view.jsp and edit_foo.jsp files include the init.jsp shown in the previous section. Therefore, they can access the fooLocalService variable which references the service component object. The view.jsp file uses the component’s getFoosCount method and getFoos method in a Liferay Search Container that lists Foo instances.

<liferay-ui:search-container
    total="<%= fooLocalService.getFoosCount() %>"
>
    <liferay-ui:search-container-results
        results="<%= fooLocalService.getFoos(searchContainer.getStart(), searchContainer.getEnd()) %>"
    />
    ...
</liferay-ui:search-container>

The edit_foo.jsp file calls getFoo(long id) to retrieve a Foo entity based on the entity instance’s ID.

long fooId = ParamUtil.getLong(request, "fooId");
Foo foo = null;
if (fooId > 0) {
    foo = fooLocalService.getFoo(fooId);
}

Using the @Reference annotation, you can inject your application’s OSGi DS components (such as a portlet DS component) with instances of your application’s Service Builder-generated local service components. Also you can provide your JSPs access to the component instances via RenderRequest attributes.

Creating Local Services

Invoking Local Services

Invoking Local Services from Spring Service Builder Code

OSGi Services and Dependency Injection with Declarative Services

« Integrating with Liferay's FrameworksInvoking Services from Spring Service Builder Code »
¿Fue útil este artículo?
Usuarios a los que les pareció útil: 0 de 0