Pages

Sunday 28 October 2012

Create a Scheduler in Liferay

Create a Scheduler in Liferay
In this small blog we will give an understanding of how can we write a Scheduler to run a cron job for performing a background job on the portal. 

Step 1 : Create a scheduler class in your portlet

import com.liferay.portal.kernel.messaging.Message;
import com.liferay.portal.kernel.messaging.MessageListener;
import com.liferay.portal.kernel.messaging.MessageListenerException;

public class NewsLetterScheduler implements MessageListener {

  public void receive(Message arg0) throws MessageListenerException {
       System.out.println("newsletter received!!!");
        /* Add your business logic here*/
  }
}

Step 2 : Specify the path in the liferay-portlet.xml.

The class name specified in the liferay-portlet.xml must match with scheduler class.

<portlet>
<portlet-name>newsletter-portlet</portlet-name>
<icon>/icon.png</icon>
<scheduler-entry>
    <scheduler-description>
       scheduler will run on every 15 minutes
    </scheduler-description>
    <scheduler-event-listener-class>
      com.sa.newsletter.trigger.NewsLetterScheduler</scheduler-event-listener-class>
 
   <trigger>
       <simple>
          <simple-trigger-value>15</simple-trigger-value>
          <time-unit>minute</time-unit>
       </simple>
  
        <!--<cron>
            <cron-trigger-value>0 0 0 * * ?</cron-trigger-value>
        </cron>
-->

  </trigger>
</scheduler-entry>
</portlet>

This (<cron-trigger-value>0 0 0 * * ?</cron-trigger-value>) indicates that the scheduler will run every night at 00 hrs.

<cron>
    <cron-trigger-value>0 0 0 * * ?</cron-trigger-value>
</cron>



I Hope this small blog helps you a lot!!!

Sunday 14 October 2012

Add a portlet to the Liferay Control Panel

Add a portlet to the Liferay Control Panel
If you want your portlet to be available in the liferay control panel, just add the following line in your liferay-portlet.xml:


<control-panel-entry-category>content</control-panel-entry-category>
<control-panel-entry-weight>15</control-panel-entry-weight>



  • control-panel-entry-category: The category where your plugin portlet will appear.
    • There are total 4 values you can use as per your requirements for category element.
      1. my
      2. content
      3. portal
      4. server
  • control-panel-entry-category: Determine the relative position in your relevant category portlet.The higher the number, the lower in the list your portlet will appear within that category. 
  • control-panel-entry-class: This is optional and is used to define rights (Who can see the portlet in the control panel). 


Make sure your portlet is not instanciable if you want your portlet in control panel.

add below line to your liferay-portlet.xml (for make portlet not instanciable).

<instanciable>false</instanciable>

I hope it helps you guys.... :)


Liferay Configuration in windows with Tomcat

Liferay Configuration in windows with Tomcat

Solution Analysts


In this brief tutorial, we will discuss how to setup Liferay with Apache tomcat Web Server.


Requirements before getting started #

  • JAVA (JDK & JRE)
  • Eclipse IDE
  • Liferay Tomcat bundle (Community Edition or Enterprise Edition)
  • Liferay Plug-in SDK
  • Apache Ant (with ecj.jar if not having)


Step -1: Download & Installation

1) To setup machine for Liferay development first of all download JDK from below link and install.

2) Download eclipse from below link.

3) Download Liferay plug-in SDK and Tomcat bundle from below link.

4) Download Apache ant version 1.8.1 from below link.

That’s all about downloading and installation bundles for development.


Step -2: Setting UP Environment Variables

Set JAVA_HOME:



Set LIFERAY_HOME:



Set ANT_HOME:




Step - 3:  Eclipse Configuration for Liferay Development

 First open eclipse and follow the instruction below,
1) Go to Help menu --> Eclipse Marketplace
2) Find here “Liferay” and just install Liferay IDE.



Install Liferay IDE and restart eclipse.


Step - 4:  Liferay Plug-ins SDK Setup

1) Open eclipse with Liferay IDE installed.
2) Open Preference page for Liferay (Go to window ->preferences -> Liferay -> Installed plugin SDK).



3) Here press the add button.
4) Browse to the location of your Plug-ins SDK installation.
5) Select OK and you should see your SDK in the list of installed SDKs.
6) Here, at the bottom of dialog screen, you will have option to update build. <username>.properties file. Select here prompt.

Congratulations! Your system is ready with development environment for Liferay.


Note: multiple SDKs can be added to the preferences but you will need to select at least one SDK to be the default.

Step – 5: Liferay Portal: Apache Tomcat Runtime/Server Setup #

          1) From eclipse go to Window àPreferences à Server àRuntime environments.


        
        2) Now, Click on Add to add a new Liferay runtime and find Liferay V6.1 tomcat under the     Liferay, Inc. and press next button.
           
            

      3) Now, set the Liferay tomcat directory here, also set the runtime JRE (Liferay v6.1 CE(tomcat 7) JRE) and press next button.


          
          4) Set here Liferay tomcat bundle zip file. And press Finish.




Step – 6: Set build.username.properties file

Set here tomcat directory path if not configured properly.

app.server.type=tomcat
          app.server.dir=${project.dir}/../bundles/tomcat-7.0.23
          app.server.deploy.dir=${app.server.dir}/webapps
          app.server.lib.global.dir=${app.server.dir}/lib/ext
          app.server.portal.dir=${app.server.dir}/webapps/ROOT

Step – 7: Create new Liferay Plug-in Project

1)   Now, a SDK and portal server have been configured. You can create a new Liferay plug-in project.

Go to file àNew à Liferay project.

2)     In the Liferay Plug-in project page, First add the project name and display name. after that select the SDK and Liferay runtime and then select the plug-in  type (Portal is default) and you can create a new plug-in project by clicking finish.





3)     If it worked fine than you should see a new plug-in project in the package explorer, so you are to begin Plug-in development.



Step – 8: Deploy Portlet

1)   Right click on portlet plug-in project àselect LiferayàSDKàdeploy


2) If deployment is successful than you got the message in console"BUILD SUCCESSFUL"


Step – 8: Start Apache tomcat server

1)    Run startup.bat from your Liferay tomcat bundle bin directory.
     
    wait till server start up.


    


3)    if server start successful, browser should open to the portal home at http://localhost:8080






Saturday 13 October 2012

How to fetch Liferay's entity using DynamicQuery or Finder class

How to fetch Liferay's entity using DynamicQuery or Finder class
How to fetch Liferay's entity using DynamicQuery?


I was using the DynamicQuery but getting the error "Unable" to find model class.

was trying to find the issue and I got the solution for the same.

I am sharing the solution with you Guys,

To allow your DynamicQuery to access the implementation class:

Below are the three different scenario to access the implementation class.

1) If your implementation class is in your portlet.

Ans: You don't need to use special classloader. It's will work out of the box. 

2) If the implementation class is the part of the portal .

Ans: Use the below line:
       
Classloader loader = PortalClassLoaderUtil.getClassLoader();

3) If the implementation class is in another portlet.

Ans: Use the below line:
        
ClassLoader classLoader = (ClassLoader)PortletBeanLocatorUtil.locate(ClpSerializer.SERVLET_CONTEXT_NAME,"portletClassLoader");

 Hope it helps! :)