DWR "push"

Source: Internet
Author: User

 

Many related examples are found on the internet, but most of them are dwr2 examples. The results are average. I studied dwr3.0 and made a demo to push content to one or more precise targets.

The focus is to add scriptsessionlistener to scriptsessionmanager to listen to the creation and destruction of scriptsession.

In scriptsessionlistener, there are two methods to be implemented: sessioncreated and sessiondestroyed, which correspond to the creation and destruction of scriptsession respectively. You can add the required content to the method.

 

I configured a servlet in Web. XML to enable auto-running listening.

Initscriptsession. Java

[Java]
View plaincopyprint?
  1. Import javax. servlet. genericservlet;
  2. Import javax. servlet. servletcontext;
  3. Import javax. servlet. servletrequest;
  4. Import javax. servlet. servletresponse;
  5. Import javax. servlet. http. httpsession;
  6. Import org. directwebremoting. container;
  7. Import org. directwebremoting. scriptsession;
  8. Import org. directwebremoting. servercontextfactory;
  9. Import org. directwebremoting. webcontextfactory;
  10. Import org. directwebremoting. event. scriptsessionevent;
  11. Import org. directwebremoting. event. scriptsessionlistener;
  12. Import org. directwebremoting. Extend. scriptsessionmanager;
  13. Public class initscriptsession extends genericservlet {
  14. Servletcontext application;
  15. Public void Init (){
  16. Container = servercontextfactory. Get (). getcontainer ();
  17. Scriptsessionmanager manager = container
  18. . Getbean (scriptsessionmanager. Class );
  19. Scriptsessionlistener listener = new scriptsessionlistener (){
  20. Public void sessioncreated (scriptsessionevent eV ){
  21. Httpsession session = webcontextfactory. Get (). getsession ();
  22. Scriptsession = eV. getsession ();
  23. String userid = session. getattribute ("userid"). tostring ();
  24. Scriptsession. setattribute ("userid", userid );
  25. System. Err. println ("create ---" + scriptsession. GETID () + "------- put userid into scriptsession"
  26. + Userid );
  27. }
  28. Public void sessiondestroyed (scriptsessionevent eV ){
  29. System. Out. println ("Destroy -----" + eV. getsession (). GETID ());
  30. }
  31. };
  32. Manager. addscriptsessionlistener (listener );
  33. }
  34. Public void Service (servletrequest req, servletresponse res ){
  35. Init ();
  36. }
  37. }

Import javax. servlet. genericservlet; <br/> Import javax. servlet. servletcontext; <br/> Import javax. servlet. servletrequest; <br/> Import javax. servlet. servletresponse; <br/> Import javax. servlet. HTTP. httpsession; </P> <p> Import Org. directwebremoting. container; <br/> Import Org. directwebremoting. scriptsession; <br/> Import Org. directwebremoting. servercontextfactory; <br/> Import Org. directwebremoting. webcontextfactory; <br/> Import Org. directwebremoting. event. scriptsessionevent; <br/> Import Org. directwebremoting. event. scriptsessionlistener; <br/> Import Org. directwebremoting. extend. scriptsessionmanager; </P> <p> public class initscriptsession extends genericservlet {</P> <p> servletcontext application; </P> <p> Public void Init () {<br/> Container = servercontextfactory. get (). getcontainer (); <br/> scriptsessionmanager = container <br/>. getbean (scriptsessionmanager. class); <br/> scriptsessionlistener listener = new scriptsessionlistener () {<br/> Public void sessioncreated (scriptsessionevent eV) {<br/> httpsession session = webcontextfactory. get (). getsession (); <br/> scriptsession = eV. getsession (); <br/> string userid = session. getattribute ("userid "). tostring (); <br/> scriptsession. setattribute ("userid", userid); <br/> system. err. println ("create ---" + scriptsession. GETID () + "------- put userid into scriptsession as" <br/> + userid); <br/>}</P> <p> Public void sessiondestroyed (scriptsessionevent eV) {<br/> system. out. println ("Destroy -----" + eV. getsession (). GETID (); <br/>}< br/>}; <br/> manager. addscriptsessionlistener (listener); <br/>}</P> <p> Public void Service (servletrequest req, servletresponse res) {<br/> Init (); <br/>}< br/>}

 

In the sessioncreated method, the ID of the current login user is put into the scriptsession. This ID is used to identify the push target during the subsequent push process.

 

Push method:

[Java]
View plaincopyprint?
  1. /**
  2. * Push to a specified user
  3. * @ Param userid
  4. * @ Param request
  5. * @ Return
  6. */
  7. Private void send (final string receiverid, final string MSG ){
  8. Browser. withallsessionsfiltered (New scriptsessionfilter (){
  9. Public Boolean match (scriptsession session ){
  10. If (session. getattribute ("userid") = NULL)
  11. Return false;
  12. Else
  13. Return (session. getattribute ("userid"). Equals (receiverid );
  14. }
  15. }, New runnable (){
  16. Public void run (){
  17. Collection <scriptsession> colls = browser. gettargetsessions ();
  18. For (scriptsession: colls ){
  19. Scriptsession. addscript (initfunctioncall (
  20. "DWR. util. setvalue", "info", MSG ));
  21. }
  22. }
  23. });
  24. }

/** <Br/> * push to the specified user <br/> * @ Param userid <br/> * @ Param request <br/> * @ return <br/> * /<br/> private void send (final string receiverid, final string MSG) {<br/> browser. withallsessionsfiltered (New scriptsessionfilter () {<br/> Public Boolean match (scriptsession session) {<br/> If (Session. getattribute ("userid") = NULL) <br/> return false; <br/> else <br/> return (Session. getattribute ("userid ")). equals (receiverid); <br/>}< br/>}, new runnable () {<br/> Public void run () {<br/> collection <scriptsession> colls = browser. gettargetsessions (); <br/> for (scriptsession: colls) {<br/> scriptsession. addscript (initfunctioncall (<br/> "DWR. util. setvalue "," info ", MSG); <br/>}< br/>}); <br/>}

 

Other methods are similar to those found on the Internet. If you need them, you can go to the Internet to study them.

 

The solution focuses on creating a new scriptsession every time the page is refreshed.

Because the creation mechanism of scriptsession is different from that of httpsession, it will be re-created every time the page is refreshed, And the destruction mechanism will be automatically destroyed only after the connection is lost or fails for a certain period of time, this may cause the server to save a lot of useless scriptsessions, so it will not only affect the performance, but more importantly, it may not implement the functions you want.
The solution is to call a DWR method on the page where you call DWR. Engine. setactivereverseajax (true.

 

DWR. Engine. setnotifyserveronpageunload (true );

 

The function of this method is to destroy the current scriptsession when the page is destroyed or refreshed. This ensures that no invalid scriptsession object exists in the scriptsession set obtained by the server.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.