• Save
Turning the Web on its head - let's have the Server call the Client
Upcoming SlideShare
Loading in...5
×
 

Turning the Web on its head - let's have the Server call the Client

on

  • 3,782 views

We will take the accepted view that a Web-Client calls (via HTTP GET or POST) a server and turn it on its head. Let\'s have the server call the client. It is really not that far fetched, to imaging a ...

We will take the accepted view that a Web-Client calls (via HTTP GET or POST) a server and turn it on its head. Let\'s have the server call the client. It is really not that far fetched, to imaging a scenario where the server, when it has determined that something exciting just happened (e.g., the Dow Jones Industrial Avg. Index jumped 100 points) calls the Web Client, instead of clients constantly polling the server (even when the DOW barely moves).

Statistics

Views

Total Views
3,782
Views on SlideShare
1,985
Embed Views
1,797

Actions

Likes
1
Downloads
0
Comments
0

7 Embeds 1,797

http://wolfpaulus.com 1776
http://www.linkedin.com 7
http://www.slideshare.net 6
http://translate.googleusercontent.com 3
http://www.google.ca 3
http://webcache.googleusercontent.com 1
http://www.google.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Turning the Web on its head - let's have the Server call the Client Turning the Web on its head - let's have the Server call the Client Presentation Transcript

    • Let’s have the server call the clientLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Polling Client Server Did something just happen ? Notify Client Server Something just happened !Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • QuoteService Number getQuote(String symbol); Set<String> getCoveredStocks();Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • package com.carlsbadcubes.quotes; import java.util.Set; public interface QuoteService { Number getQuote( String symbol ); Set<String> getCoveredStocks(); }Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Exposing the QuoteService: Hessian Binary Web Service ProtocolLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • <?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> defined in hessian.jar <servlet-name>qservice</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <init-param> Implementation <param-name>service-class</param-name> <param-value>com.carlsbadcubes.quotes.QuoteServiceImpl</param-value> </init-param> <init-param> <param-name>api-class</param-name> Interface <param-value>com.carlsbadcubes.quotes.QuoteService</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>qservice</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping> </web-app>Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Hessian Client String url = “http://hostname.domain/service”; ServiceProxyFactory factory = new HessianProxyFactory(); QuoteService qs = (QuoteService) factory.create( QuoteService.class, url );Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Demo Tomcat Server with a Hessian Service and a Hessian based Java ClientLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Tomcat Server Client Jan 18, 2008 11:43:11 AM org.apache.coyote.http11.Http11Protocol init INFO: Initializing Coyote HTTP/1.1 on http-8080 Jan 18, 2008 11:43:11 AM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 1460 ms Jan 18, 2008 11:43:11 AM org.apache.catalina.core.StandardService start INFO: Starting service Catalina Jan 18, 2008 11:43:11 AM org.apache.catalina.core.StandardEngine start INFO: Starting Servlet Engine: Apache Tomcat/6.0.14 Jan 18, 2008 11:43:14 AM org.apache.coyote.http11.Http11Protocol start INFO: Starting Coyote HTTP/1.1 on http-8080 Jan 18, 2008 11:43:14 AM org.apache.jk.common.ChannelSocket init INFO: JK: ajp13 listening on /0.0.0.0:8009 Jan 18, 2008 11:43:14 AM org.apache.jk.server.JkMain start INFO: Jk running ID=0 time=0/105 config=null Jan 18, 2008 11:43:14 AM org.apache.catalina.startup.Catalina start INFO: Server startup in 3046 ms Connected to serverLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com daniel.gredler.net
    • Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com daniel.gredler.net
    • When debugging with a TCP-Monitor, use “Bulap” instead of “Hessian.Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Polling Client Server Did something just happen ? Notify Client Server Something just happened !Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • JMX supports event-handling over multiple JVMsLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • JMX supports event-handling over multiple JVMsLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • package com.carlsbadcubes.mbeans; import java.util.Map; Interface name ends in ...MBean public interface SystemInfoMBean { Long getFreeMemory(); /** @return Long, max memory, JVM will attempt to use. */ Long getMaxMemory(); /** @return Long total amount of memory in the JVM. */ Long getTotalMemory(); /** Runs the garbage collector. */ void runGC(); /** @return Map current system properties. */ Map getSystemInfo(); }Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Class implements an interface with the class’ name +”MBean” package com.carlsbadcubes.mbeans; import java.util.Map; /** SystemInfo delegates incoming requests to System or Runtime. */ public class SystemInfo implements SystemInfoMBean { ... /** @return Long, total amount of memory in the JVM. */ public Long getTotalMemory() { return Runtime.getRuntime().totalMemory(); } /** Runs the garbage collector. */ public void runGC() { Runtime.getRuntime().gc(); } /** @return Map, current system properties. */ public Map getSystemInfo() { return System.getProperties(); } }Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • JMX supports event-handling over multiple JVMsLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • import javax.management.*; // Create an MBeanServer private MBeanServer mbServer = MBeanServerFactory.createMBeanServer( “mydomain” ); // Creating an MBean mbServer.createMBean( SystemInfo.class.getName(), new ObjectName( ":name=SystemInfo,type=system" ) );Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • JMX supports event-handling over multiple JVMsLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • import javax.management.remote.*; JMXServiceURL jmxurl = new JMXServiceURL( "rmi", "localhost", 9090, "/jndi/rmi://"+InetAddress.getLocalHost().getHostName()+":1099/jmxapp”); JMXConnectorServer connector = ! JMXConnectorServerFactory.newJMXConnectorServer(jmxurl,null,mbServer); connector.start(); Example JMXServiceURL: service:jmx:rmi://localhost:9090/jndi/rmi://wpbook.local:1099/jmxappLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • JMX-Demo Server: MBeanServer / JMXConnector(RMI) Client: Java’s JConsoleLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Server ClientLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • JMXClient public class JMXClient implements NotificationListener { private MBeanServerConnection mbsc; private ObjectName oname = new ObjectName("com.carlsbadcubes:name=PrimeGenerator,type=custom"); public JMXClient( String serviceUrl ) throws Exception { JMXServiceURL jmxurl = new JMXServiceURL( serviceUrl ); JMXConnector connector = JMXConnectorFactory.connect( jmxurl ); mbsc = connector.getMBeanServerConnection(); mbsc.addNotificationListener( oname, this, null, null ); } ... }Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • JMXClient // // Implement NotificationListener Interface // public void handleNotification(Notification note, Object object ) { System.out.println( note.getSequenceNumber() + ". type= " + note.getType() + " msg= " + note.getMessage() + " data= " + note.getUserData() ); }Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • MBeanServerConnection mbsc = connector.getMBeanServerConnection(); ObjectName oname = new ObjectName("com.carlsbadcubes:name=SystemInfo,type=custom"); ... mbsc = connector.getMBeanServerConnection(); Long free = (Long) mbsc.getAttribute( oname, "FreeMemory" ); import com.carlsbadcubes.mbeans.SystemInfoMBean; ... MBeanServerConnection mbsc = connector.getMBeanServerConnection(); ObjectName oname = new ObjectName("com.carlsbadcubes:name=SystemInfo,type=custom"); ... mbsc = connector.getMBeanServerConnection(); SystemInfoMBean sysinfo = (SystemInfoMBean)MBeanServerInvocationHandler.newProxyInstance( mbsc, oname, SystemInfoMBean.class, false ); Long free = sysinfo.getFreeMemory();Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • JMX-Demo Server: MBeanServer / JMXConnector(RMI) Client: Custom Java Client ApplicationLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • JMXConnector JMXConnectorServer RMI JMX JNDI RMI Registry Client Server JMX SocketSvr MBeanServerConnection MBeanServerLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • JMXConnector JMXConnectorServer HTTP HessianClient Hessian Invoker Servlet Client Server MBeanServerConnection MBeanServerLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • JMX / Hessian Demo Server: MBeanServer / JMXConnector(HESSIAN) Client: Specialized Java ClientLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Client Server Did something just happen ? Polling Something happened ! Notify JMX Client What happened ? Server HTTP This happened. HTTPLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • AJAX, Comet, and NIOLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Poll Long Poll (AJAX Push)Client Server Client Server Request Request Response Event Response Request Request Responsetime timeLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> blocking & non-blocking IO configuration<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="20000" redirectPort="8443" />Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • public class QuoteServlet extends HttpServlet implements CometProcessor, Listener { protected final ArrayList<CometEvent> connections = new ArrayList<CometEvent>(); /** * Process the given Comet event. * @param event The Comet event that will be processed * @throws java.io.IOException * @throws ServletException */ public void event(CometEvent event) throws IOException, ServletException { if (CometEvent.EventType.BEGIN.equals(event.getEventType())) {.. connections.add(event); } else if (CometEvent.EventType.ERROR.equals(event.getEventType())) .. } else if (CometEvent.EventType.END.equals(event.getEventType())) .. } else if (CometEvent.EventType.READ.equals(event.getEventType())) .. } } // Implement Quotes Listener Interface public void notify(String message) { if (message != null) { // JSON-style message containing ticker and value e.g.: { "ticker": AAPL, "value" : 123.45 } messageSender.send("{"ticker":"" + message + "", "value":"" + qservice.getQuote(message) + ""}"); } } /** Sends an X-JSON message in the HTTP Response Header and flushes the response and closes the connection.*/ public class MessageSender implements Runnable { protected final ArrayList<String> messages = new ArrayList<String>(); public void run() { // Send any pending message on all the open connections for (int i = connections.size() - 1; 0 <= i; i--) { CometEvent event = connections.get(i); event.getHttpServletResponse().addHeader("X-JSON", pendingMessages[0]); } // endof run } // endof class MessageSender }Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • AJAX / Comet Demo Server: Comet-Style Web Application (non-blocking) Client: Modern Web BrowserLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Thanks For Coming!Let’s have the server call the client Copyright © 2009 http://wolfpaulus.com
    • Resources Slides: http://wolfpaulus.com/bio/talks/ahr.pdf Code: http://wolfpaulus.com/bio/talks/ahr.zip Hessian • http://hessian.caucho.com/ • http://wiki.caucho.com/Hessian JMX • http://java.sun.com/products/JavaManagement/best-practices.html • JMX Accelerated Howto by Blaine Simpson http://www.admc.com/blaine/howtos/jmx/ MX4J Remoting • http://mx4j.sourceforge.net/docs/ch03.html Non-Blocking I/O • http://tomcat.apache.org/tomcat-6.0-doc/aio.htmlLet’s have the server call the client Copyright © 2009 http://wolfpaulus.com