Designing Scalable High Performance Rich Clients from the Trenches Rob Ratcliff Partner/Consultant/Lead Engineer Futuretek Net Services LLC
Total Page:16
File Type:pdf, Size:1020Kb
Designing Scalable High Performance Rich Clients From the Trenches Rob Ratcliff Partner/Consultant/Lead Engineer FutureTek Net Services LLC http://www.futuretek.com/ TS-3723 2007 JavaOneSM Conference | Session TS-3723 | Session Goals Design lessons from the trenches Learn practical design lessons from the development of a challenging networked rich client application. 2007 JavaOneSM Conference | Session TS-3723 | 2 About FutureTek LLC • Founded in 1995 • Projects and Technologies • High performance rich networked Swing clients • Distributed Multi-Disciplinary Optimization Framework for Boeing, MDOPT • CORBA integration • Database applications • Java™ technology-based web projects • On-line ordering, radio show scheduling, call center, inventory management • Multi-player trivia game applet • Co-founded Austin Java Users Group http://www.austinjug.org 2007 JavaOneSM Conference | Session TS-3723 | 3 Agenda Overview of the “NEWS” Project Architecture Overview Comparison and Contrast of Design Solutions War Stories and Lessons Learned Summary 2007 JavaOneSM Conference | Session TS-3723 | 4 NEWS Project Background • Design GUI to control a network of sensors and display the resulting engineering data in near real time • Overall Design Approach • Mockup screens using NetBeans™ software to allow customer to interact using mock data • Java Platform Swing Toolkit for GUI (rather than AWT) • Stateful high-performance binary protocol and asynchronous messaging • Simple command+name/value pair control language • Socket per session mimicking a distributed service factory (made demultiplexing of events easier too) • Develop mock server to accelerate development 2007 JavaOneSM Conference | Session TS-3723 | 5 Application Display Requirements • Support streaming data (positions and speed, signal, status, audio etc.) • Display time—varying positions and other sensor-related data on a world map • Display data in time order • Synchronize all views in time • Synchronize selection in all views • Minimal mouse clicks to access functions • Display all relevant data up front 2007 JavaOneSM Conference | Session TS-3723 | 6 Design Scalability Defined • Feature • Development • GUI Layout • Practice type safety first • Toolbar space (avoid string based paradigms) • Menu Structure • Ease editing through IDE • Screen real estate auto-completion • Feature Navigation • Ease refactoring • Hooking in new components • Ease debugging through compile-time error checking • Leveraging what others have done • GUI-builder and XML driven screens for easier long term • Duration (long execution maintenance runs and multiple sessions) • Deployment • Memory Performance • Easy configuration as number • CPU Performance of settings increase • GUI Responsiveness • Simple software update 2007 JavaOneSM Conference | Session TS-3723 | 7 NEWS Initial Mockup Rapidly Generated Using NetBeans Software GUI Builder 2007 JavaOneSM Conference | Session TS-3723 | 8 NEWS Networked Sensor Application 2007 JavaOneSM Conference | Session TS-3723 | 9 DEMO NEWS Intruder Detection Scenario 2007 JavaOneSM Conference | Session TS-3723 | 10 Architecture Forensics GUI Visual Layer Preferences Component Lookup Toolbars Tables Data Plots Dialogs Information Panels Data Selection Widgets Menus Plot Models Plot Interaction Controls Application Layer Queues TM Proxy Event Bus Session Model Event Generation & Parser Layer NMSG UFF SNL Network Layer Reader Writer Queue 2007 JavaOneSM Conference | Session TS-3723 | 11 I’m an Enterprise Unto Myself Large GUI applications share many of the problems that distributed component architectures have • Complex threading issues • Lookup and hookup with “distant” components and models • Asynchronous notification • Transactions • Persistence • Data mappings and graph navigation 2007 JavaOneSM Conference | Session TS-3723 | 12 Where in the World Is My Component? • Dependency Injection (Constructor or Setter) • Singleton • BeanContext • Lookup Services • Event Bus 2007 JavaOneSM Conference | Session TS-3723 | 13 Dependency Injection Before Injection Was Cool SessionContext idea borrowed from the earlier Enterprise JavaBeans™ (EJB™) specification 2007 JavaOneSM Conference | Session TS-3723 | 14 SessionContext Life-cycle management • Creation • Create Queues and Worker Threads • Data Models • Register data models with event bus • Component listener hookup • Destruction • Thread shutdown • Socket disconnect • Component listener removal (to avoid memory leaks) 2007 JavaOneSM Conference | Session TS-3723 | 15 Session Life-cycle Client :SessionManager component:Sessionable getInstance() createSessionContext() * s:SessionContext * p:TMProxy * bus:EventBus * n:Network createSessionContext() setSessionContext() addModelListeners() clearSessionContext() disconnect() removeModelListeners() 2007 JavaOneSM Conference | Session TS-3723 | 16 Domain Specific Data Model Advantages Sorted tree map used to keep • Easy to navigate records in time • Domain specific data rules and state order Disadvantages • More coupling due to dependencies • Harder to test Usage: sessionContext.getNetwork() .getLocalSensor() .getPvtModel().getRecords(); 2007 JavaOneSM Conference | Session TS-3723 | 17 Singleton Example Type-Safe preferences management • Goal: Make preferences easily accessible to programmer without having to know magic strings • Type safe façade class to access properties for a given module • Use one property file Note: JavaBeans™ specification BeanInfo framework very useful for dynamically setting and serializing property values and auto-generating property sheets 2007 JavaOneSM Conference | Session TS-3723 | 18 Singleton Tradeoffs Advantages Disadvantages • Simple concept • Leads to tighter • Globally accessible coupling • Ensures one • Ensures one instance instance 2007 JavaOneSM Conference | Session TS-3723 | 19 BeanContext Architecture and Navigation • Based on composite design pattern Note: OpenMap framework built on java.beans.BeanContext infrastructure 2007 JavaOneSM Conference | Session TS-3723 | 20 Sample BeanContext Interfaces and Support Classes and Consumers 2007 JavaOneSM Conference | Session TS-3723 | 21 Adding a Bean to the BeanContext Note: The OpenMap framework delegates all added context change notifications to the findAndInitMethod(Object o) to reduce the number of user-implemented methods 2007 JavaOneSM Conference | Session TS-3723 | 22 BeanContext Tradeoffs Advantages Disadvantages • Flexible composite • Rather large set of structure interfaces and methods to • Part of the standard Java implement Development Kit (JDK) distribution • A bit complicated • Allowed easy integration • Custom code required to with the OpenMap support hierarchical framework notifications and searches 2007 JavaOneSM Conference | Session TS-3723 | 23 Example Lookup Services • NetBeans software style Lookup • (ChatProxy)Lookup.lookup(ChatProxy.class); • (ChatProxy)Lookup.lookup( new Template(ChatProxy.class, “mychat”)); • Java Naming and Directory Interface™ (J.N.D.I.) API Lookup • (ChatProxy)context.lookup(objectName); • (ChatProxy)directory.search(contextName, attributes) 2007 JavaOneSM Conference | Session TS-3723 | 24 Keep Asynchronous “Remote Calls” Simple Through Synchronicity • Listener and correlation ID pattern to block for response when protocol is asynchronous or message based • Foxtrot keeps GUI lively (Concurrent worker vs. Single worker) • Deliver events in correct thread 2007 JavaOneSM Conference | Session TS-3723 | 25 Synchronous Pub/Sub Paradigm with Asynchronous Communications How about embedding Java Message Service (JMS) into a rich client? 2007 JavaOneSM Conference | Session TS-3723 | 26 InvokeLater Insanity “Doing the same thing over and over and expecting different results”—Ben Franklin • Expecting developers to consistently use SwingUtilities.invokeLater() is wishful thinking at best • Framework should take care of this automatically 2007 JavaOneSM Conference | Session TS-3723 | 27 Communications Using Event Bus • Listeners implementing SwingListener interface receive delivered events on EDT • Inter-component communication • Listeners register for Swing events by type • Events pushed onto bus 2007 JavaOneSM Conference | Session TS-3723 | 28 Using Event Bus for Inter- Component Communication Tradeoffs Advantages Disadvantages • Supports multiple • Proliferation of producers of a given event types event type • No knowledge of • Slower performance producer required than direct method calls • Can hook up directly • Demultiplex desired with producer via producer “source” of event 2007 JavaOneSM Conference | Session TS-3723 | 29 How Does Foxtrot Work? 2007 JavaOneSM Conference | Session TS-3723 | 30 Embedding Foxtrot into Remote Interface Proxy (Stub) Using Dynamic Proxy ChatProxyHandler chatProxyHandler = new ChatProxyHandler(chatProxyDelegate); ChatCommand proxy = (ChatCommand) Proxy.newProxyInstance(loader, new Class[] { ChatCommand.class }, chatProxyHandler); 2007 JavaOneSM Conference | Session TS-3723 | 31 Dynamic Proxy (Cont.) class ChatProxyHandler implements InvocationHandler { public Object invoke(Object proxy, final Method method, final Object[] args) { this.fireBusy(true); Object value = null; try { if (!SwingUtilities.isEventDispatchThread()) { value = method.invoke(delegate, args); } else { value = ConcurrentWorker.post( new Task() { public Object run() throws Exception { return method.invoke(delegate, args); } }); } } finally { this.fireBusy(false); } return value; } } 2007 JavaOneSM Conference | Session TS-3723 | 32 Global Selection Notification • Desired