Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Arko Mahmud
DWR and Reverse Ajax Support
August 6, 2013 11:27 AM
Answer

Arko Mahmud

Rank: Junior Member

Posts: 43

Join Date: June 11, 2013

Recent Posts

Hello
I was trying to implement DWR reverse ajax in Liferay:

Here The following steps I have taken...

In a Custom Class mentioned in dwr.xml :

  1package com.testing.Dwr;
  2
  3import java.util.Date;
  4import java.util.concurrent.ScheduledThreadPoolExecutor;
  5import java.util.concurrent.TimeUnit;
  6
  7import org.directwebremoting.Browser;
  8import org.directwebremoting.ScriptSession;
  9import org.directwebremoting.ScriptSessionFilter;
 10import org.directwebremoting.ScriptSessions;
 11import org.directwebremoting.ServerContextFactory;
 12import org.directwebremoting.WebContextFactory;
 13import org.directwebremoting.impl.DaemonThreadFactory;
 14import org.directwebremoting.ui.dwr.Util;
 15
 16
 17public class TryDwr implements Runnable {
 18   
 19     protected transient boolean active = false;
 20     protected String timeString = "";
 21   
 22   
 23
 24    public TryDwr(){
 25       
 26        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory());
 27        executor.scheduleAtFixedRate(this, 1, 50, TimeUnit.MILLISECONDS);
 28       
 29        }
 30
 31
 32
 33    @Override
 34    public void run() {
 35        // TODO Auto-generated method stub
 36         if (active)
 37            {
 38                String newTimeString = new Date().toString();
 39                // We check this has not already been sent to avoid duplicate transmissions
 40                if (!newTimeString.equals(timeString))
 41                {
 42                    setClockDisplay(newTimeString);
 43                    timeString = newTimeString;
 44                }
 45            }
 46       
 47    }
 48   
 49    /**
 50     * Called from the client to turn the clock on/off
 51     */
 52   
 53     public synchronized void toggle()
 54        {
 55            active = !active;
 56            setClockStatus();
 57        }
 58   
 59     private class UpdatesEnabledFilter implements ScriptSessionFilter
 60        {
 61            private String attrName;
 62            
 63            public UpdatesEnabledFilter(String attrName)
 64            {
 65                this.attrName = attrName;
 66            }
 67            
 68            public boolean match(ScriptSession ss)
 69            {
 70                Object check = ss.getAttribute(attrName);
 71                return (check != null && check.equals(Boolean.TRUE));
 72            }       
 73        }
 74   
 75    
 76     /**
 77         * Send the time String to clients that have an UPDATES_ENABLED_ATTR attribute set to true
 78         * on their ScriptSession.
 79         *
 80         * @param output The string to display.
 81         */
 82        public void setClockDisplay(final String output)
 83        {
 84            Browser.withAllSessionsFiltered(new UpdatesEnabledFilter(UPDATES_ENABLED_ATTR), new Runnable()
 85            {
 86                public void run()
 87                {
 88                    Util.setValue("clockDisplay", output);
 89                }
 90            });
 91        }
 92
 93    
 94     /**
 95         * Call a function on the client for each ScriptSession.
 96         * passing the clock's status for display.
 97         *
 98         * @param output The string to display.
 99         */
100        public void setClockStatus()
101        {
102            Browser.withAllSessions(new Runnable()
103            {
104                public void run()
105                {
106                    ScriptSessions.addFunctionCall("setClockStatus()", active);
107                }
108            });
109        }
110       
111        public void setEnabledAttribute(Boolean enabled) {
112            ScriptSession scriptSession = WebContextFactory.get().getScriptSession();
113            scriptSession.setAttribute(UPDATES_ENABLED_ATTR, enabled);
114        }
115       
116        private static String UPDATES_ENABLED_ATTR = "UPDATES_ENABLED";
117       
118   
119}


An empty Portlet Class
I will customize it later with ajax request, this is empty as its a Demo for now...

JSP view.jsp

 1<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
 2
 3<portlet:defineObjects />
 4
 5This is the <b>Dwr Check</b> portlet in View mode.
 6
 7<script>
 8
 9window.onload=function()
10{
11    dwr.engine.setActiveReverseAjax(true);
12    dwr.engine.setErrorHandler(errorHandler);
13    dwr.engine.setPollStatusHandler(updatePollStatus);
14    updatePollStatus(true);
15    Tabs.init('tabList', 'tabContents');      
16}
17      
18function errorHandler(message, ex)
19{
20    dwr.util.setValue("error", "Cannot connect to server. Initializing retry logic.", {escapeHtml:false});
21    setTimeout(function() { dwr.util.setValue("error", ""); }, 5000)
22}
23      
24function updatePollStatus(pollStatus)
25{
26    dwr.util.setValue("pollStatus", pollStatus ? "Online" : "Offline", {escapeHtml:false});
27}
28      
29function enableUpdates(enabled)
30{
31    if (!enabled)
32    {
33        dwr.util.setValue("clockDisplay", "This tab/window does not have updates enabled.");
34    } 
35    Clock.setEnabledAttribute(enabled);
36}
37
38function setClockStatus(clockStatus) {
39    dwr.util.setValue("clockStatus", clockStatus ? "Clock started" : "Clock stopped");
40}
41
42</script>
43
44
45<div id="demoDiv">      
46    <input type="button" value="Start / Stop" onclick="Clock.toggle();"/>
47    Server status: <span id="pollStatus"></span>
48    <div id="error"></div>
49    <p></p>
50    <h2 id="clockDisplay"></h2>
51  </div>
52  <div>
53    <h3>Filtering</h3>
54    <input type="button" value="Enable Updates" onclick="enableUpdates(true);">
55    <input type="button" value="Disable Updates" onclick="enableUpdates(false);">      
56  </div>   


Its not giving any error, But I doesn't enable clockDisplay.....

P.S. Codes are copied from DWR website just to understand mechanism. And implement in Liferay.....

Please suggest, I am new in Liferay, browsed in internet to find any solution. i also read through documentations.

BR
Arko
Arko Mahmud
RE: DWR and Reverse Ajax Support
August 7, 2013 1:28 AM
Answer

Arko Mahmud

Rank: Junior Member

Posts: 43

Join Date: June 11, 2013

Recent Posts

Could anyone give any hints at least....
Sampsa Sohlman
RE: DWR and Reverse Ajax Support
August 7, 2013 5:12 AM
Answer

Sampsa Sohlman

LIFERAY STAFF

Rank: Regular Member

Posts: 217

Join Date: September 27, 2007

Recent Posts

Hi Arko,

Have you managed to get DWR working with Liferay without reverse ajax.

I have past worked with DWR, but that was many years ago. If I remember right DWR did have servlet as ajax communication interface and it was very powerful.

With Liferay environment it might be good to configure that dwr servlet through PortalDelegateServlet interface at you plugin's web.xml

Remember, when you are developing portal apps, apps should stay at their container. So use namespace where required.
Arko Mahmud
RE: DWR and Reverse Ajax Support
August 7, 2013 7:17 AM
Answer

Arko Mahmud

Rank: Junior Member

Posts: 43

Join Date: June 11, 2013

Recent Posts

Hello Sampsa
Thanks for your reply. Unfortunately I could not figure it out.
According to your advice I tried to put following code

 1    <servlet>
 2        <servlet-name>dwr-invoker</servlet-name>
 3        <servlet-class>com.liferay.portal.kernel.servlet.PortalDelegateServlet</servlet-class>
 4
 5        <init-param>
 6            <param-name>Servlet-class</param-name>
 7            <param-value>org.directwebremoting.servlet.DwrServlet</param-value>
 8        </init-param>
 9
10
11   
12        <load-on-startup>1</load-on-startup>
13    </servlet>


But it gave exception.

It seems very few people are aware with DWR. Can you please suggest me any Library that would help me to implement comet in Liferay portlet. I could not find any solution for libraries like atmosphere or DWR (In Liferay environment).
All I need is to consume message from web services and update browser using comet, for one of my university project.

Sytem: Liferay 6.1 tomcat-bundle.

Thanks again
Arko
Sampsa Sohlman
RE: DWR and Reverse Ajax Support
August 8, 2013 12:07 AM
Answer

Sampsa Sohlman

LIFERAY STAFF

Rank: Regular Member

Posts: 217

Join Date: September 27, 2007

Recent Posts

Arko Mahmud:
But it gave exception.


I got familiar with DWR back in 2005 or 2006 (don't remember exact). I was actually surprised that 3.0 has not gone out yet, due that I got little bit more instrested of this matter and I made also little experiments. You need delegate servlet if you are using Liferay services like ThemeDisplay, but if you are not then it's not needed. Still as I quickly tried, I did not manage to make it work either due DWR url generation.

If you want to investigate further and get rid of exception add:

1        <init-param>
2          <param-name>sub-context</param-name>
3          <param-value>dwr</param-value>
4        </init-param> 


to your web.xml and call the <your server>/delegate/dwr/

but then you see that there should be away to override DWR context path recognition (I did not dig in to that since I do not want to invest time for this).

Arko Mahmud:
It seems very few people are aware with DWR. Can you please suggest me any Library that would help me to implement comet in Liferay portlet. I could not find any solution


Our wiki has some documentation of comet, but I my self have not done any implementation with it. See following wiki page:

http://www.liferay.com/community/wiki/-/wiki/1071674/Comet+Integration

May be someone else could help more about using comet.

Remember that our ServiceBuilder does also generate remote services, but there are no server push, but you should implement that by your self by example polling.
Arko Mahmud
RE: DWR and Reverse Ajax Support
August 9, 2013 5:29 PM
Answer

Arko Mahmud

Rank: Junior Member

Posts: 43

Join Date: June 11, 2013

Recent Posts

Hi
Could you answer in this thread please... http://www.liferay.com/community/forums/-/message_boards/message/27099297 I really need some help to understand DWR.

Thanks in advance...
Ankit _
RE: DWR and Reverse Ajax Support
September 11, 2013 1:36 AM
Answer

Ankit _

Rank: Junior Member

Posts: 46

Join Date: October 18, 2012

Recent Posts

Hi Arko Mahmud ,

Have you find any solution for DWR ajax comet ? I need to implement comet/reverse ajax functionality for my project.

Thanks,
Ankit
Arko Mahmud
RE: DWR and Reverse Ajax Support
September 30, 2013 7:55 AM
Answer

Arko Mahmud

Rank: Junior Member

Posts: 43

Join Date: June 11, 2013

Recent Posts

Hei,
Do you still need any support ? If yes than check my other threads, You will find the solution ;)
Good Luck!!