In this tutorial, you’ll learn about the differences between local and remote services, and when you should invoke local services rather than their remote service counterparts.
Once Service Builder has generated your module project’s services, you can call
them from anywhere in your application. When invoking a local service, you
should call one of the service methods of a *LocalService
class. You should
never call the service methods of an *Impl
class directly. Local services in
your project are generated automatically when using Service Builder. To do this,
set the local-service
attribute to true
for an entity in the service.xml
file. Service Builder generates methods that call existing services, but you can
create new methods in the *LocalServiceImpl
class that can be generated into
new exposed methods in your module’s services (*LocalService
,
*LocalServiceUtil
, etc.).
Many of Liferay’s module applications use their generated remote services for important calls in their controller layer, for example, because they offer conveniences like configured permissions checking. Remote services perform a permission check and then invoke the corresponding local service. However, there are many services you’d like to expose only to your local project, and do not want other applications to have access to.
In the Bookmarks application, for example, the
BookmarksEntryLocalService
interface provides the openEntry
method, which opens a bookmark for viewing.
The remote service interface
BookmarksEntryService,
however, does not
provide this method. Why could this be?
There are many services that you don’t want to expose to other applications in Liferay. In the example mentioned above, Bookmarks are configured to only open locally, meaning that other apps do not have access to open and view a bookmark entry. This should only be done by the Bookmarks application. Therefore, in certain cases, you’ll need to invoke local services instead of remote services. For more information on invoking remote services, see the Invoking Remote Services tutorial.
To see how you could call a local service from a portlet action class, you’ll
examine the
EditOrganizationMVCActionCommand
class. Notice that this class has a private instance variable called
_dlAppLocalService
. The _dlAppLocalService
instance variable of type
DLAppLocalService
gets an instance of DLAppLocalService
at runtime via
dependency injection. The instance variable is set like this:
@Reference(unbind = "-")
protected void setDLAppLocalService(DLAppLocalService dlAppLocalService) {
_dlAppLocalService = dlAppLocalService;
}
This tutorial demonstrated how you can call the local services generated by Service Builder in your project. To learn how to call Liferay services, see the Service Security Layers and Finding and Invoking Liferay Services tutorials.