Foros de discusión

Service Builder 101?

Ken Turner, modificado hace 11 años.

Service Builder 101?

New Member Mensajes: 16 Fecha de incorporación: 20/07/12 Mensajes recientes
Service Builder looks interesting, but I'm not having much success in finding out what is really going on and how to use it. I have read (several times) "Liferay in Action" and various online tutorials, but they don't tell me what I need to know to get started. The examples that are given tend to focus on particular applications rather than explaining the fundamental principles.

In the hope that it will help someone to provide a more basic tutorial in future, I have given below some of the beginner questions that could usefully be answered.

(1) Service Builder is said to use Hibernate. However I am not using this database, so it is unclear whether the approach will work with (say) Hypersonic or MySQL.

(2) It appears that Service Builder generates code for web services. Is that true only for remote use? I would wonder about the efficiency of web services for purely local use.

(3) As far as I can tell, Service Builder allows me to manipulate data objects (entities) which then get automatically persisted in a database without requiring any specific actions by the programmer. If so, then there are presumably no "initialize" and "finalize" actions. If my interpretation is correct, this needs to be explained right up front in the Service Builder description.

(4) Where is "service.xml" fully explained (apart from syntactic aspects in the DTD)? Should I consider <entity> as defining a database table, or perhaps a Java class corresponding to a data object? Is <namespace> meant to be like an XML namespace, or is it just a prefix for automatically generated names? Do I need to define <finder> elements, or are these just a convenience? Do I have to use "companyId" and "groupId" elements if I just need a non-instanceable application?

(5) I have successfully written a "service.xml" file and translated this. I now have a bewildering number of classes with complex relationships (base, implementation, interface, ...). The entry point into these classes is unclear. Some methods are static while others require class instances. I cannot tell what the key classes are and how to use them. Specifically, how should I create instances of my entities, retrieve them, change them and delete them? How should a portlet make use of the generated classes?

(6) What is the significance of the JAR file that is created in addition to all the generated classes? Can it be ignored, or is it relevant in some circumstances?

(7) Various tutorials extend "XyzLocalServiceImpl". Is it necessary to put anything in this class, or are the extensions an artefact of the tutorial examples? I suspect that it's not necessary at all, and is needed only if some convenience methods are required.

Thanks!
thumbnail
Brian Kim, modificado hace 11 años.

RE: Service Builder 101?

Expert Mensajes: 311 Fecha de incorporación: 17/08/04 Mensajes recientes
I'm going to try to help answer these questions in piecemeal as I have time. Hopefully others will also chime in.

(1) Service Builder is said to use Hibernate. However I am not using this database, so it is unclear whether the approach will work with (say) Hypersonic or MySQL.


Liferay's persistence layer is built to leverage Hibernate. Hiberate is not a database. In fact, one of the primary benefits (amongst others) of Hibernate is that an application can be database agnostic. You do not want to use Hypersonic unless you're planning on just using it for demo purposes. As such, Liferay supports the same databases that Hibernate supports. See here for the full list.
Ken Turner, modificado hace 11 años.

RE: Service Builder 101?

New Member Mensajes: 16 Fecha de incorporación: 20/07/12 Mensajes recientes
If you and others can pick these questions off one by one, that would be very helpful. I've seen others ask for a basic tutorial on Service Builder, so this will help beginners (like me).

(1) Thank you for the clarification. A colleague had misinformed me about the nature of Hibernate. The possible relevance to me of Hypersonic/HSQLDB is that it's convenient for teaching purposes in a computer lab.
thumbnail
Brian Kim, modificado hace 11 años.

RE: Service Builder 101?

Expert Mensajes: 311 Fecha de incorporación: 17/08/04 Mensajes recientes
Hi Ken,

At the risk of sounding "salesy", you may want to consider attending one of our developer courses. They'll walk you through how Service Builder works, but more importantly, teach you how to do things the right way when developing in Liferay. And yes, you can definitely do things the wrong way and make your life harder down the road.

http://www.liferay.com/services/training
thumbnail
Brian Kim, modificado hace 11 años.

RE: Service Builder 101?

Expert Mensajes: 311 Fecha de incorporación: 17/08/04 Mensajes recientes
(2) It appears that Service Builder generates code for web services. Is that true only for remote use? I would wonder about the efficiency of web services for purely local use.


They are not just for remote use (although it does generate classes for remote calling too in case you need it). Web tier classes can make calls to the service tier classes that are generated without its being "web services". So you don't need to worry about inefficiency. The classes generated are actually a means of abstraction, which leads me to answering one of your other questions...

(5) I have successfully written a "service.xml" file and translated this. I now have a bewildering number of classes with complex relationships (base, implementation, interface, ...). The entry point into these classes is unclear. Some methods are static while others require class instances. I cannot tell what the key classes are and how to use them. How should a portlet make use of the generated classes?


The abstraction is intentional. By doing it this way, you allow for the implementation class (*Impl.java) to be abstracted from the calling class. You only need to worry about which Util class to call, and that is dependent on how you're trying to call it. If you're just trying to call it from your own portlet that resides in the Web tier, then just call the *LocalServiceUtil class. I think that part is pretty well explained in the Liferay in Action book as well as here.

Specifically, how should I create instances of my entities, retrieve them, change them and delete them?

Still can't find it? Search for the part that has:
Book book = bookPersistence.create(bookId);


If you're still wondering why all the classes are being generated, my answer is that it's to simplify your life. If you wanted, you could avoid using Service Builder altogether. There's nothing proprietary with the code that is generated. At the end of the day, it's just Java that's being generated. But if you really wanted to write everything yourself, well, get ready to do a lot of work. You'll need to deal with transaction management, make sure that you pull the database connection from the database pool, write your own persistence classes, wrapper classes, etc. Service Builder is just trying to help you focus on the business logic and not worry about all the "extra" stuff that has been done over and over again. But again, it's just a tool. You can choose to use it or not.
thumbnail
Jack Bakker, modificado hace 11 años.

RE: Service Builder 101?

Liferay Master Mensajes: 978 Fecha de incorporación: 3/01/10 Mensajes recientes
Talking about music is like dancing about architecture.

That said, my library of books on Liferay is now getting extensive and I really like Liferay in Action. The forums (wikis/blogs) here are also very useful (an understatement).

In the grander scheme of things, service builder is such a time saver. To get to know it, it is worth reading a bit (books/online), staring at the DTD, using the Liferay IDE/Studio to speed dev up ; and learning to add methods to the service-builder generated *model.impl source and also to your service-builder generated *service.impl source.
Ken Turner, modificado hace 11 años.

RE: Service Builder 101?

New Member Mensajes: 16 Fecha de incorporación: 20/07/12 Mensajes recientes
Many thanks to everyone for the answers here, which clarify a lot of things. A couple of small responses to clarify my original questions:

(1) Actually, I've been developing complex software systems for a long time. Among the many languages and packages I've used I just hadn't come across Hibernate, and when I asked a colleague about it I got the wrong answer.

(3) A common pattern for many applications is open, process, close. It is therefore usual to have to initialise something (say, open a database connection), use it (say, issue SQL queries), and finalise it (say, close a database connection). I couldn't tell whether the Service Builder approach required this, e.g. whether there was some "init" method to be called or implemented.

I should now be able to make progress on this. Thanks emoticon
thumbnail
Hitoshi Ozawa, modificado hace 11 años.

RE: Service Builder 101?

Liferay Legend Mensajes: 7942 Fecha de incorporación: 24/03/10 Mensajes recientes
I think you want to know how transaction are handled when using service builder. :-)
Smilelws2010 lwz, modificado hace 10 años.

RE: Service Builder 101?

Regular Member Mensajes: 160 Fecha de incorporación: 16/12/10 Mensajes recientes
Very informative thread. Thanks to everyone contributed.

Yes, I would like to know how the transaction in general is carried out. I know the persistence classes will take care of that using Hibernate, but not a hibernate pro. I would like to understand (a quick brief) how it is making connection to database in an agnostic way.

And in particular worst case), say your database is down, how we can track that project a message to customer about that.

Thanks
Smile
thumbnail
David H Nebinger, modificado hace 11 años.

RE: Service Builder 101?

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
Ken Turner:
(1) Service Builder is said to use Hibernate. However I am not using this database, so it is unclear whether the approach will work with (say) Hypersonic or MySQL.


Obviously new to development, too, if you don't know Hibernate. Hibernate is an object relational mapping tool which you use so you don't have to do all of the JDBC result set processing on your own.

Hibernate is (for the most part) database agnostic and layers on top of both Hypersonic and Mysql.

(2) It appears that Service Builder generates code for web services. Is that true only for remote use? I would wonder about the efficiency of web services for purely local use.


Yes. For local, you'd use the XxxLocalServiceUtil class to access your service builder entities and methods.

(3) As far as I can tell, Service Builder allows me to manipulate data objects (entities) which then get automatically persisted in a database without requiring any specific actions by the programmer. If so, then there are presumably no "initialize" and "finalize" actions. If my interpretation is correct, this needs to be explained right up front in the Service Builder description.


Not sure what you're asking for here.

(4) Where is "service.xml" fully explained (apart from syntactic aspects in the DTD)? Should I consider <entity> as defining a database table, or perhaps a Java class corresponding to a data object? Is <namespace> meant to be like an XML namespace, or is it just a prefix for automatically generated names? Do I need to define <finder> elements, or are these just a convenience? Do I have to use "companyId" and "groupId" elements if I just need a non-instanceable application?


The DTD is the best documentation for the various XML entities. If you use the Liferay IDE, it has a nice visual editor which can make creating service.xml files a little less verbose.

An entity represents a row of a table. Namespace is used when creating new tables to prefix the table name in the entity object (so you can use a prefix of "MYCOMP" and then use "USER_" as a table name knowing that you will not face collisions with Liferay's "USER_" table.

Finders are a convenience to hide the details of the actual finder implementation rather than having to hand-code all of the various sorts of finders you need.

Company id and group id are used when you need to support Liferay's multi-hosting/multi-group capabilities. For example, if a logical record may have different values for different hosts, the company id will make it a unique row. Same sort of concept with the group; if a record should have one value for group A but a different value for group B, the groupId element will give you a unique row. These have nothing to do with 'non-instanceable applications'.

(5) I have successfully written a "service.xml" file and translated this. I now have a bewildering number of classes with complex relationships (base, implementation, interface, ...). The entry point into these classes is unclear. Some methods are static while others require class instances. I cannot tell what the key classes are and how to use them. Specifically, how should I create instances of my entities, retrieve them, change them and delete them? How should a portlet make use of the generated classes?


The concept that you're not getting is that the generated code is meant to handle crossing the class loader boundary that exists between deployed applications in an application container. Normally an instance of a class cannot be passed to another web application in the same container; class loader issues prevent that. The bulk of the code generated by Service Builder gets around all of these issues so you can effectively share entities across the class loader boundaries.

As far as which classes you should use, it is all down to the XxxLocalServiceUtil classes. Only use that class and the interfaces that it exposes.

When developing your own code, refer to the generated comments in the code that tell you where your changes should be made.

(6) What is the significance of the JAR file that is created in addition to all the generated classes? Can it be ignored, or is it relevant in some circumstances?


This is the service jar for the service you created. It contains the XxxLocalServiceUtil classes and relevant interfaces that other plugins need to access your service. It can only be ignored by plugins that have no need of your service. If your plugin needs the service, it needs the service jar.

(7) Various tutorials extend "XyzLocalServiceImpl". Is it necessary to put anything in this class, or are the extensions an artefact of the tutorial examples? I suspect that it's not necessary at all, and is needed only if some convenience methods are required.


Extension is usually used when you need to change functionality in the existing Liferay service builder entities. By extending their classes you can override their methods with your own. In addition, you end up w/ a change to Liferay's spring configuration to use your extended instance rather than your own.

When you are dealing with your own service entities, extension is not needed and should not be used.