Fórumok

DWR and Reverse Ajax Support

thumbnail
Arko Mahmud, módosítva 10 év-val korábban

DWR and Reverse Ajax Support

Junior Member Bejegyzések: 43 Csatlakozás dátuma: 2013.06.11. Legújabb bejegyzések
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 :

package com.testing.Dwr;

import java.util.Date;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ScriptSessionFilter;
import org.directwebremoting.ScriptSessions;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.impl.DaemonThreadFactory;
import org.directwebremoting.ui.dwr.Util;


public class TryDwr implements Runnable {
	
	 protected transient boolean active = false;
	 protected String timeString = "";
	
	

	public TryDwr(){
		
		ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory());
        executor.scheduleAtFixedRate(this, 1, 50, TimeUnit.MILLISECONDS);
		
		}



	@Override
	public void run() {
		// TODO Auto-generated method stub
		 if (active)
	        {
	            String newTimeString = new Date().toString();
	            // We check this has not already been sent to avoid duplicate transmissions
	            if (!newTimeString.equals(timeString))
	            {
	                setClockDisplay(newTimeString);
	                timeString = newTimeString;
	            }
	        }
		
	}
	
	/**
     * Called from the client to turn the clock on/off
     */
	
	 public synchronized void toggle()
	    {
	        active = !active;
	        setClockStatus();
	    }
	
	 private class UpdatesEnabledFilter implements ScriptSessionFilter
		{
	    	private String attrName;
	    	
	    	public UpdatesEnabledFilter(String attrName)
		    {
	    		this.attrName = attrName;
	    	}
	    	
	        public boolean match(ScriptSession ss)
		    {
				Object check = ss.getAttribute(attrName);
		        return (check != null && check.equals(Boolean.TRUE));
			}    	
	    }
	
	 
	 /**
	     * Send the time String to clients that have an UPDATES_ENABLED_ATTR attribute set to true 
	     * on their ScriptSession.
	     * 
	     * @param output The string to display.
	     */
	    public void setClockDisplay(final String output)
	    {
	        Browser.withAllSessionsFiltered(new UpdatesEnabledFilter(UPDATES_ENABLED_ATTR), new Runnable()
	        {
	            public void run()
	            {
	                Util.setValue("clockDisplay", output);
	            }
	        });
	    }

	 
	 /**
	     * Call a function on the client for each ScriptSession.
	     * passing the clock's status for display.
	     * 
	     * @param output The string to display.
	     */
	    public void setClockStatus()
		{
	        Browser.withAllSessions(new Runnable()
	        {
	            public void run()
	            {
	                ScriptSessions.addFunctionCall("setClockStatus()", active);
	            }
	        });
	    }
	    
	    public void setEnabledAttribute(Boolean enabled) {
	    	ScriptSession scriptSession = WebContextFactory.get().getScriptSession();
	    	scriptSession.setAttribute(UPDATES_ENABLED_ATTR, enabled);
	    }
	    
	    private static String UPDATES_ENABLED_ATTR = "UPDATES_ENABLED";
	    
	
}


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

JSP view.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineobjects />

This is the <b>Dwr Check</b> portlet in View mode.

<script>

window.onload=function()
{
    dwr.engine.setActiveReverseAjax(true);
    dwr.engine.setErrorHandler(errorHandler);
    dwr.engine.setPollStatusHandler(updatePollStatus);
    updatePollStatus(true);
    Tabs.init('tabList', 'tabContents');       
}
	  
function errorHandler(message, ex) 
{
    dwr.util.setValue("error", "Cannot connect to server. Initializing retry logic.", {escapeHtml:false});
	setTimeout(function() { dwr.util.setValue("error", ""); }, 5000)
}
	  
function updatePollStatus(pollStatus) 
{
    dwr.util.setValue("pollStatus", pollStatus ? "Online" : "Offline", {escapeHtml:false});
}
	  
function enableUpdates(enabled)
{
    if (!enabled) 
	{
	    dwr.util.setValue("clockDisplay", "This tab/window does not have updates enabled.");
	}  
    Clock.setEnabledAttribute(enabled);
}

function setClockStatus(clockStatus) {
    dwr.util.setValue("clockStatus", clockStatus ? "Clock started" : "Clock stopped");
}

</script>


<div id="demoDiv">  	
    <input type="button" value="Start / Stop" onclick="Clock.toggle();">
	Server status: <span id="pollStatus"></span>
	<div id="error"></div>
    <p></p>
    <h2 id="clockDisplay"></h2>
  </div>
  <div>
    <h3>Filtering</h3>
	<input type="button" value="Enable Updates" onclick="enableUpdates(true);">
	<input type="button" value="Disable Updates" onclick="enableUpdates(false);">  	
  </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
thumbnail
Arko Mahmud, módosítva 10 év-val korábban

RE: DWR and Reverse Ajax Support

Junior Member Bejegyzések: 43 Csatlakozás dátuma: 2013.06.11. Legújabb bejegyzések
Could anyone give any hints at least....
thumbnail
Sampsa Sohlman, módosítva 10 év-val korábban

RE: DWR and Reverse Ajax Support

Regular Member Bejegyzések: 230 Csatlakozás dátuma: 2007.09.27. Legújabb bejegyzések
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.
thumbnail
Arko Mahmud, módosítva 10 év-val korábban

RE: DWR and Reverse Ajax Support

Junior Member Bejegyzések: 43 Csatlakozás dátuma: 2013.06.11. Legújabb bejegyzések
Hello Sampsa
Thanks for your reply. Unfortunately I could not figure it out.
According to your advice I tried to put following code

	<servlet>
		<servlet-name>dwr-invoker</servlet-name>
		<servlet-class>com.liferay.portal.kernel.servlet.PortalDelegateServlet</servlet-class>

		<init-param>
			<param-name>Servlet-class</param-name>
			<param-value>org.directwebremoting.servlet.DwrServlet</param-value>
		</init-param>


	
		<load-on-startup>1</load-on-startup>
	</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
thumbnail
Sampsa Sohlman, módosítva 10 év-val korábban

RE: DWR and Reverse Ajax Support

Regular Member Bejegyzések: 230 Csatlakozás dátuma: 2007.09.27. Legújabb bejegyzések
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:

		<init-param>
		  <param-name>sub-context</param-name>
		  <param-value>dwr</param-value>
		</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.
thumbnail
Arko Mahmud, módosítva 10 év-val korábban

RE: DWR and Reverse Ajax Support

Junior Member Bejegyzések: 43 Csatlakozás dátuma: 2013.06.11. Legújabb bejegyzések
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 _, módosítva 10 év-val korábban

RE: DWR and Reverse Ajax Support

Junior Member Bejegyzések: 46 Csatlakozás dátuma: 2012.10.18. Legújabb bejegyzések
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
thumbnail
Arko Mahmud, módosítva 10 év-val korábban

RE: DWR and Reverse Ajax Support

Junior Member Bejegyzések: 43 Csatlakozás dátuma: 2013.06.11. Legújabb bejegyzések
Hei,
Do you still need any support ? If yes than check my other threads, You will find the solution ;)
Good Luck!!