Wednesday, August 14, 2013

Liferay DWR portlet

Download portlet from following link


Steps to create DWR Portlet

Create MVC portlet using Liferay IDE
Configure DWR serve let in  portlet web.xml file.
<servlet>
                        <servlet-name>dwr-invoker</servlet-name>
                        <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
                        <init-param>
                                    <param-name>debug</param-name>
                                    <param-value>true</param-value>
                        </init-param>
                        <init-param>
                                    <param-name>activeReverseAjaxEnabled</param-name>
                                    <param-value>true</param-value>
                        </init-param>
                        <init-param>
                                    <param-name>crossDomainSessionSecurity</param-name>
                                    <param-value>false</param-value>
                        </init-param>
                        <init-param>
                                    <param-name>initApplicationScopeCreatorsAtStartup</param-name>
                                    <param-value>true</param-value>
                        </init-param>
                        <init-param>
                                    <param-name>maxWaitAfterWrite</param-name>
                                    <param-value>-1</param-value>
                        </init-param>
                        <load-on-startup>1</load-on-startup>
            </servlet>
            <servlet-mapping>
                        <servlet-name>dwr-invoker</servlet-name>
                        <url-pattern>/dwr/*</url-pattern>
            </servlet-mapping>

Create dwr.xml file in WEB-INF directory of your portlet
Add following tags in dwr.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">

<dwr>
  <allow>
  
  </allow>
</dwr>

Create Java class based on your need

In my example my java class is com.meere.dwr.Person.java

Configure java class in dwr.xml files

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">

<dwr>
  <allow>
   <create creator="new" javascript="Person">
      <param name="class" value="com.meera.dwr.Person"/>
    </create>
  </allow>
</dwr>

Access your java class in your jsp page i.e.  Client side

Add following java script file in your jsp

<script type='text/javascript' src='<%=renderRequest.getContextPath()%>/dwr/interface/Person.js'> </script>
<script type='text/javascript' src='<%=renderRequest.getContextPath()%>/dwr/engine.js'> </script>

Note:

In above Person.js and engine.js file physically not existed any where  don’t worry .

And whatever the attribute value you used for JavaScript in dwr.xml file  by that name your client side java script will be created i.e. in my case Person.js

dwr.xml

<create creator="new" javascript="Person">
      <param name="class" value="com.meera.dwr.Person"/>
    </create>

Java script call in jsp page

<script type='text/javascript' src='<%=renderRequest.getContextPath()%>/dwr/interface/Person.js'> </script>

Engine.js is common for all….

<script type='text/javascript' src='<%=renderRequest.getContextPath()%>/dwr/engine.js'> </script>

Final code in jsp page

<script type='text/javascript' src='<%=renderRequest.getContextPath()%>/dwr/interface/Person.js'> </script>
<script type='text/javascript' src='<%=renderRequest.getContextPath()%>/dwr/engine.js'> </script>
<h1>Sample Liferay DWR portlet</h1>
<button id="checkDwr">GetName Using DWR</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#checkDwr").click(function(){
         Person.getData(42, {
                callback:function(str) {
                  alert(str);
                }
              });
  });
});
</script>


Liferay version 6.1x

More details go through following links




Saturday, August 3, 2013

Liferay Plugin portlet connecting to multiple data bases/Data Sources

Liferay Plugin portlet connecting to multiple data bases/Data Sources


Lifray portal 6.1GA2+Tomcat7.X+Mysql 5.1X

Download Portlet from following URL


Place the portlet into your plugins/portlet directory

Create portlet using Liferay IDE from existing portlet and select portlet what you have downloaded.

Note:

 before import please delete .project, .classpath and .settings  files from downloaded portlet.

Create   data base in mysql database  name is “anotherdatabase”  

Create Table name is TableFromAnotherDataSource in “anotherdatabase”  database.

CREATE TABLE `tablefromanotherdatasource` (
            `IFADSId` BIGINT(20) NOT NULL,
            `Description` VARCHAR(75) NULL DEFAULT NULL,
            PRIMARY KEY (`IFADSId`)
);

Add following properties in portal-ext.properties file

jdbc.anotherdbconfig.driverClassName=com.mysql.jdbc.Driver
jdbc.anotherdbconfig.url=jdbc:mysql://localhost:3306/anotherdatabase?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.anotherdbconfig.username=root
jdbc.anotherdbconfig.password=

Note:

When we change portal-ext.proprties file you have to stop server after add some properties you have to restart.

portal-ext.properties file should be available in liferay home path if not you have to create and add above properties.

Generally home path like below

/bundles/ portal-ext.proprties  or liferay-portal-6.1.1-ce-ga2/ portal-ext.proprties 

Run ant build-service target from your eclipse ant view

Run and deploy then portle will be deployed

Note: 
this portlet developed in Loferay Portal 6.1GA2 version and Plug-in SDK 6.1GA2

This portlet will use to connect to multiple data bases. When we get requirement plugin portlet need to connect to multiple databases or data sources we have to configure data source and Session Factory information in ext-spring.xml. 

We have to create xml file and we need to place file in src/META-INF/ext-spring.xml

Generally Liferay plugin portlets uses default liferay database or data source. If we want connect to another database we have to configure those details in ext-spring.xml file.

Once we configured the data source and session factory in ext-spring.xml file then have to use these data source information in service.xml file that’s for entity.

following is ext-spring.xml

<?xml version="1.0"?>
<beans default-destroy-method="destroy" default-init-method="afterPropertiesSet"
       xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

       <bean id="anotherDataSource"
              class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
              <property name="targetDataSource" ref="anotherDataSourceWrapper" />
       </bean>
       <bean id="anotherDataSourceImpl"
              class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
              <property name="propertyPrefix" value="jdbc.anotherdbconfig." />
       </bean>
       <bean id="anotherDataSourceWrapper" class="com.liferay.portal.dao.jdbc.util.DataSourceWrapper">
              <constructor-arg ref="anotherDataSourceImpl" />
       </bean>
       <bean class="com.liferay.portal.dao.jdbc.util.DataSourceSwapper">
              <property name="liferayDataSourceWrapper" ref="anotherDataSourceWrapper" />
       </bean>
       <bean id="anotherHibernateSessionFactory" class="com.liferay.portal.kernel.spring.util.SpringFactoryUtil"
              factory-method="newBean">
              <constructor-arg                  value="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration" />
              <constructor-arg>
                     <map>
                           <entry key="dataSource" value-ref="anotherDataSource" />
                     </map>
              </constructor-arg>
       </bean>
       <bean id="anotherSessionFactory" class="com.liferay.portal.kernel.spring.util.SpringFactoryUtil"
              factory-method="newBean">
              <constructor-arg
                     value="com.liferay.portal.dao.orm.hibernate.PortletSessionFactoryImpl" />
              <constructor-arg>
                     <map>
                           <entry key="dataSource" value-ref="anotherDataSource" />
                           <entry key="sessionFactoryClassLoader" value-ref="portletClassLoader" />
                           <entry key="sessionFactoryImplementor" value-ref="anotherHibernateSessionFactory" />
                     </map>
              </constructor-arg>
       </bean>
</beans>


Once we configured data source and session factory in ext-spring.xml we have to provide data source properties from portal-ext.properties file which in you liferay home path.

Generally home path like below

/bundles/ portal-ext.proprties  or liferay-portal-6.1.1-ce-ga2/ portal-ext.proprties 

Add the following properties in portal-ext.proprties 

jdbc.anotherdbconfig.driverClassName=com.mysql.jdbc.Driver
jdbc.anotherdbconfig.url=jdbc:mysql://localhost:3306/anotherdatabase?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
 jdbc.anotherdbconfig.username=root
 jdbc.anotherdbconfig.password=


Note: 

jdbc. anotherdbconfig. Is the propertyPrefix which we have mentioned in ext-spring.xml file


<bean id="anotherDataSourceImpl"
                        class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
                        <property name="propertyPrefix" value="jdbc.anotherdbconfig." />
</bean>


Now we have to provide this information to entity which we have configured in service.xml .

In entity tag we have two attributes from that we can explicitly said about data source and session factory information .

<entity name="TableFromAnotherDataSource" table="TableFromAnotherDataSource"  local-service="true" remote-service="true" data-source="anotherDataSource" session-factory="anotherSessionFactory">
                        <column name="IFADSId" type="long" primary="true" />
                        <column name="Description" type="String" />
            </entity>


Note: 
we have table attribute in entity tag when we mention this entity class targeting to that particular table

If we not provide table attribute then service builder create table name as namespace_entityname

If provide table attribute then table will be created exact name that we provided in table attribute.

Example:

We already having existed data base then we need not create tables from service builder then we just pass table name in table attribute then out entity class targeting to that table.

Observations:

Note: 

When we use other data source for plugin portlet when we run service builder then tables creation script is not create so we have to create table manually in database if the table is new.

If you need create table script you can see in the class XXXModelImpl.java

XXX= entity Name

public static final String TABLE_SQL_CREATE = "create table TableFromAnotherDataSource (IFADSId LONG not null primary key,Description VARCHAR(75) null)";

You can also find data source and session factory that is used by your Entity class

public static final String DATA_SOURCE = "anotherDataSource";
public static final String SESSION_FACTORY = "anotherSessionFactory";


Friday, March 8, 2013

Liferay search container with curd operations.


Liferay search container with curd operations.

I create port let which explained how to use life ray search container to display records as grid.
I added some of records in person tables and I display all records in liferay grid and when click on any row it will navigate to update form, once we finished upadate it will navigate to search container page.
The following the link to get search container portlet.

Steps to run the portlet:
1.      Download portlet
2.      Place the portlt into liferau plugins/portlet directory / if you are use Liferay IDE create liferay project from existing source
3.      Run ant build-service
4.      Run ant deploy
5.      See the sample category and drag and drop the port let
6.      If records available you can see the records otherwise add new person by click on Add new person link.
7.      When you click on liferay grid row you can update the person record.

Screen Shots:


Note: This is done in liferay6.0.6 if you are using liferay 6.1 then create your portlet and manually copy file to your portlet. When you copy portet.xml , liferay-portelt.xm. you should not copy directly because dtd version changed so make sure dtd version should acceding to liferay versions.

Friday, March 1, 2013

Connection pooling in multithreaded applications


Connection pooling in multithreaded applications

Introduction
This article talks about developing design approaches for multithreaded applications performing database operations. While developing a multithreaded database application, we always end up scratching our heads answering (or trying to answer) mischievous questions as:
  1. Who should create the connection object?
  2. Should a connection object be a property of the business object? Or should it be used as a utility?
  3. How to handle transaction operations?
  4. Who should dispose the connection object?
Looks like seven W's of Wisdoms are making enough noise in our head. I found my way of answering these questions, and I am sharing them here with you. I have tried two approaches for it, which are described here.
Single connection object, multiple transactions objects
I have used the Singleton design pattern to make sure that only one connection object is created. The same connection object is shared across multiple threads. It is now every individual thread's responsibility to handle the transaction. Every thread will create its own transaction object and will pass it to all commands it will be executing. So straight away, the thread should maintain its own commit and rollback policy. Thus, a single connection may execute multiple transactions simultaneously, wholly managed by the calling threads.
As parallel transactions are not supported by the database, make sure the code block handling the opening of the connection and transaction creation is executed under a lock mechanism. Thus, in this approach, though we can use the same connection object, we have to make sure the code is thread safe (use of lock).
Advantages:
  1. Implements a Singleton, shares one connection object across multiple calling threads.
  2. No need to dispose the connection object (but you must call the close method).
Disadvantages:
  1. Does not use the connection pool feature, as only one connection object is created and used.
  2. Increases execution time as the command must be executed using the same connection object.
Here is the block diagram for the singleton approach:
Multiple connection objects
The multiple connection approach is slightly different. This one gives the calling code control of the connection object. It becomes the calling code's responsibility to use and dispose the connection object. In an ideal scenario, the calling code (Business Layer) should not hold the reference of the connection object. It should request for the connection object when required, and use and pass it to its sub-routines, if required. Thus, the connection object is opened in a method and closed in the same one. The pseudo-code will look like:
Method (){
 - Create local connection object, open it
 - Do transaction with database.
 - Pass connection object to other methods called from this.
 - Commit or rollback transaction
 - Close connection, dispose it
}
This approach allows us to create multiple connection objects and execute them in parallel. But, it also enforces some conditions as:
  1. Calling code should take ownership of the connection object.
  2. Calling code should handle the connection pool by declaring it in the connection string.
  3. As there could be multiple connections opened simultaneously, it's the calling code which must maintain the relationship between the connection and its transaction.
Advantages:
  1. Uses connection pool to create multiple connections simultaneously.
  2. Faster compared to singleton, as multiple connections will execute their own transactions.
Disadvantages:
1.       Need to make sure that the connection object is disposed properly.
2.       Connection object needs to pass through methods.
Here is the block diagram for the multiple connection approach:

Recent Posts

Recent Posts Widget

Popular Posts