Message Boards

Best way to make an util service

Juan Pereyra, modified 3 Years ago.

Best way to make an util service

New Member Posts: 8 Join Date: 7/13/20 Recent Posts
Hi there!I'm starting to work in a mail service for my software. This need to be used in various modules. 
Which is the best aproach to build this?I use IntelliJ, and I don't see the 'DS service' recommended here (https://liferay.dev/forums/-/message_boards/message/77592272).Thanks in advance!
thumbnail
Olaf Kock, modified 3 Years ago.

RE: Best way to make an util service

Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts
Juan Pereyra:

I use IntelliJ, and I don't see the 'DS service' recommended here.

There's almost no magic in DS: You'll have a standard Java interface, and implement it in a class annotated with @Component. Then you can use it in other classes through @Reference.
I'd recommend to go through OSGi Basics, or another OSGi tutorial. Once you get the basics in, the amount of OSGi that you'll get in touch with day by day is surprisingly low and easy to understand.
thumbnail
Christoph Rabel, modified 3 Years ago.

RE: Best way to make an util service

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
A second way, if you just need want to reuse code, is to write normal classes and export them in the bnd file. We use that often for data transfer objects, since we need them in various modules.

// Note: Uses Lombok to create getters, setters
@NoArgsConstructor
@AllArgsConstructor
public class CheckDownloadDTO {

  @Getter @Setter
  private String downloadURL;
  @Getter @Setter
  private boolean downloadPossible;
...
}

You can, of course, add all kinds of methods to such classes, to create a simple library.To make them available to other modules you need to export them in the bnd.bnd file:
Export-Package: my.package.with.dto, my.otherpackage.with.something

Note: I recommend to create an extra package for dto classes. Don't add it to an existing package since it will also export stuff by default. And you will overwrite the automatically generated export directive with yours, which could cause problems. It can be fixed, but it requires some understanding what happens.
Note 2: If you use @Component/@Reference the bnd stuff is created automatically for you.
Juan Pereyra, modified 3 Years ago.

RE: Best way to make an util service

New Member Posts: 8 Join Date: 7/13/20 Recent Posts
Hi guy!.
Thanks you both!
I'm going to check that course.
The 'Simply library method' looks good! I think I'm going to give it a chance!
Thanks again!