Total Pageviews

Monday, February 28, 2011

Calling remote service method using Javascript/JSON

Liferay enables us to call the methods using Javascript. There are various advantages of it, but the most useful is - we can call a method without refreshing a the portlet (without rendering), and we can display contents as per logic.

Step 1. Firstly, generate your service for your entity keeping remote-service="true"  in service.xml

Step 2. Write a method in ServiceImpl that you wish to call from Javascript.

Step 3. Build service using ant, from ext-impl.

Step 4. Deploy the whole project.

Step 5. Method will be available in ext-web/docroot/html/js/liferay/ext_service.js.

Step 6. Include js in the jsp file you need to call the method, as follows:

<script type="text/javascript" src="/html/js/liferay/ext_service.js"></script>
Step 7. Call method as follows in javascript:

This is it... All the best...

Wednesday, February 2, 2011

Liferay Scheduler

Sometimes it is needed to create a task that is being executed periodically. We can use scheduler (based on Quartz API ) that is integrated with Liferay. There are 3 steps to do this.

1. Write a class the implements the com.liferay.portal.job.Scheduler interface

2. Write a class that contains actual task that needs to be executed. This class must implements com.liferay.portal.job.IntervalJob interface

3. Register your scheduler in liferay-portlet-ext.xml ( if you use ext environment )

Now let’s go to the details.

1. Write a class that implements Scheduler interface.

2. Write the class that contains actual task. On this example, our actual task would be only to write ‘THIS IS THE ACTUAL TASK!” to console. This task would be executed every minute.

If you want to change the interval, you can just change this line :

Change Time.MINUTE to Time.HOUR or Time.DAY.

3. Register in liferay-portlet-ext.xml.