Results 1 to 8 of 8
  1. #1
    Senior Member
    Join Date
    Jul 2009
    Location
    sofia
    Posts
    40

    Google Web Toolkit Integration

    Hi,

    Here is simple example of how to integrate lightstreamer with gwt.
    Generic class that manages dynamic control over group&schema:
    Code:
    public abstract class NonVisualGWTClient {
    
    	public enum SourceType {
    		RAW, MERGE, DISTINCT, COMMAND;
    	}
    
    	protected JavaScriptObject nvt;
    	protected String configuration;
    	protected HashMap<String, Integer> group = new HashMap<String, Integer>();
    	protected HashMap<String, Integer> schema = new HashMap<String, Integer>();
    	private JavaScriptObject page;
    	protected String sourceType = SourceType.RAW.name();
    	private JavaScriptObject config;
    	private boolean isInitialized = false;
    
    	public NonVisualGWTClient(String configuration) {
    		this.configuration = configuration;
    	}
    
    	public boolean isInitialized() {
    		return isInitialized;
    	}
    
    	public void setInitialized(boolean isInitialized) {
    		this.isInitialized = isInitialized;
    	}
    
    	private native JavaScriptObject createPushPage() /*-{
    														var page = new $wnd.PushPage(); 
    														return page;
    														}-*/;
    
    	private native void init(JavaScriptObject map, JavaScriptObject page) /*-{
    																			try {
    																			page.context.setDomain(map['domain']);
    																			page.onEngineCreation = function(engine) {
    																			engine.connection.setLSHost(map['host']);
    																			engine.connection.setLSPort(map['port']);
    																			engine.connection.setAdapterName(map['adapter_root']);
    																			engine.changeStatus(map['engine_status']);
    																			}
    																			page.bind();
    																			page.createEngine("app", map['ls_home'], "SHARE_SESSION");
    																			} catch(error) {
    																			alert(error);
    																			}
    																			}-*/;
    
    	private native JavaScriptObject getNVT(String[] group, String[] schema, String sourceType) /*-{
    																								var nonVisualTable = new $wnd.NonVisualTable(group, schema, sourceType);
    																								return nonVisualTable;
    																								}-*/;
    
    	/**
    	 * If the ls client is not initialized yet, restart() is delayed and is
    	 * called when the client is initialized
    	 * */
    	public void restart() {
    		if (isInitialized) {
    			final String[] schema = NonVisualGWTClient.this.schema.keySet().toArray(new String[0]);
    			final JavaScriptObject nvt = getNVT(NonVisualGWTClient.this.group.keySet().toArray(new String[0]), schema,
    					NonVisualGWTClient.this.sourceType);
    			this.nvt = nvt;
    			restart(nvt, this, page, config, schema);
    		}
    	}
    
    	protected native void debug(String msg) /*-{
    											alert(msg);
    											}-*/;
    
    	private native void restart(JavaScriptObject nvt, NonVisualGWTClient thiz, JavaScriptObject page, JavaScriptObject map, String [] schema) /*-{
    																															nvt.setDataAdapter(map['adapter_name']);
    																															nvt.setSnapshotRequired(false);
    																															
    																															this.@com.trinitas.ls.client.NonVisualGWTClient::nvt.onItemUpdate = function(item, itemUpdate, itemName) {
    																																	for (i=0;i<schema.length;i++) {
    																																	            try {
    																																				var msg = itemUpdate.getNewValue(schema[i]);
    																																				if (msg !=null) {
    																																					thiz.@com.trinitas.ls.client.NonVisualGWTClient::onMessage(Ljava/lang/String;Ljava/lang/String;)(msg, schema[i]);
    																																				} 
    																																				} catch(error) {
    																																				 alert(error);
    																																				}
    																																	}
    																															}
    																															
    																															page.addTable(nvt,"tnvt");
    																															}-*/;
    
    	protected abstract void onMessage(String message, String schemaId);
    	
    	public void addToShema(String item) {
    		Integer count = schema.get(item);
    		if (count == null || count == 0) {
    			schema.put(item, 1);
    		} else {
    			count = count + 1;
    			schema.put(item, count);
    		}
    	}
    
    	public Integer removeFromShema(String item) {
    		Integer count = schema.get(item);
    		if (count <= 1) {
    			schema.remove(item);
    		} else {
    			count = count - 1;
    			schema.put(item, count);
    		}
    		return count;
    	}
    
    	public void stop() {
    		group.clear();
    		schema.clear();
    		restart();
    	}
    	public void addToGroup(String item) {
    		Integer count = group.get(item);
    		if (count == null || count == 0) {
    			group.put(item, 1);
    		} else {
    			count = count + 1;
    			group.put(item, count);
    		}
    	}
    
    	public void removeFromGroup(String item) {
    		Integer count = group.get(item);
    		if (count <= 1) {
    			group.remove(item);
    		} else {
    			count = count - 1;
    			group.put(item, count);
    		}
    	}
    
    	public void initialize(final SourceType type, final AsyncCallback<Void> callback) {
    		loadLSProperties(new LightStreamerConfigHandler() {
    			@Override
    			public void afterLoad(JavaScriptObject map) {
    				try {
    					config = map;
    					NonVisualGWTClient.this.sourceType = type.name();
    					if (page == null) {
    						page = createPushPage();
    					}
    					final String [] group = NonVisualGWTClient.this.group.keySet().toArray(new String[0]); 
    					final String [] schema = NonVisualGWTClient.this.schema.keySet().toArray(new String[0]);
    					nvt = getNVT(group, schema, NonVisualGWTClient.this.sourceType);
    					if (!isInitialized) {
    						isInitialized = true;
    						init(config, page);
    					}
    					restart(nvt, NonVisualGWTClient.this, page, config, schema);
    					callback.onSuccess(null);
    				} catch (final Throwable e) {
    					callback.onFailure(e);
    				}
    			}
    		});
    	}
    
    	public HashMap<String, Integer> getGroup() {
    		return group;
    	}
    
    	//
    	// public void setGroup(HashSet<String> group) {
    	// this.group = group;
    	// }
    
    	public HashMap<String, Integer> getSchema() {
    		return schema;
    	}
    
    	// public void setSchema(HashSet<String> schema) {
    	// this.schema = schema;
    	// }
    
    //	protected abstract void setOnItemUpdate();
    
    	private void loadLSProperties(final LightStreamerConfigHandler handler) {
    		final JavaScriptObject map = readProperties(configuration);
    		handler.afterLoad(map);
    	}
    
    	private JavaScriptObject readProperties(String responseText) {
    		final JSONObject map = new JSONObject();
    		int endLineIndex;
    		for (int startIndex = 0; startIndex < responseText.length(); startIndex = endLineIndex + 1) {
    			endLineIndex = responseText.indexOf('\n', startIndex);
    			final String line = responseText.substring(startIndex, endLineIndex == -1 ? responseText.length() : endLineIndex).trim();
    			if (!"".equals(line) && !line.startsWith("#")) {
    				addNameValue(line, map);
    			}
    			if (endLineIndex == -1) {
    				break;
    			}
    		}
    		return map.getJavaScriptObject();
    	}
    
    	private void addNameValue(String line, JSONObject map) {
    		final int equalsIndex = line.indexOf('=');
    		final String key = line.substring(0, equalsIndex).trim();
    		final int hashIndex = line.indexOf('#');
    		final String value = line.substring(equalsIndex + 1, hashIndex == -1 ? line.length() : hashIndex).trim();
    		map.put(key, new JSONString(value));
    	}
    
    }
    Code:
    public interface LightStreamerConfigHandler {
    	
    	public void afterLoad(JavaScriptObject map);
    }

    For your project you must create class that consumes the messages:
    Code:
    public class MyGWTClient extends NonVisualGWTClient {
    
    	public MyGWTClient() {
    		super(ServiceUtils.getClientResource().lsconfig().getText());
    		// add here default static subscriptions - not mandatory
    		addToGroup("item1");
    		addToShema("property1");
    	}
    
    	@Override
    	protected void onMessage(String msg, String schemaId) {
                    // process message according its schema - here you may have many         //messages coming but since it is js it is not a problem
                 // example how to process message:
                  if ("property1".equals(schemaId)) {
                    // call process method for this type of message - I use eventBus to fire the  //event with that message, so the code that consumes the messages is completely //independent from ls.
                   }
    	}
    }
    I use ClientBundle to load the initial configuration of ls client.

    Code:
    	@Source("resources/ls/lsconfig.properties")
    	public TextResource lsconfig();
    Here is sample properties file:
    Code:
    domain=mycomain
    host=myhost
    port=8888
    adapter_root=TEST
    adapter_name=JMSADAPTER
    ls_home=/ls/  <!-- this is where ls config files are placed - lspushpage.js,lsengine.html,lspushpage.js and so on... -->
    engine_name=engine
    engine_status=STREAMING
    Example how to use :
    Code:
    MyGWTClient client = new MyGWTClient();
    client.initialize(SourceType.RAW, new AsyncCallback<Void>() {
    			@Override
    			public void onFailure(Throwable arg0) {
    
    			}
    
    			@Override
    			public void onSuccess(Void arg0) {
    
    			}
    		});
    client.addToGroup("item2");
    client.addToGroup("item3");
    client.addToSchema("property2");
    client.addToSchema("property3");
    client.addToSchema("property4");
    client.restart();// this makes the ls to subscribe to the above group&schema + the old ones
    // cleaning schema and group
    client.removeFromSchema("property2");
    client.removeFromGroup("item1");
    client.restart();

  2. #2
    Member
    Join Date
    Mar 2010
    Location
    Hyderabad
    Posts
    5

    Google Web Toolkit Integration

    Hi,

    Could you please tell me the modules which we have to inherit for working this code.

  3. #3
    Senior Member
    Join Date
    Jul 2009
    Location
    sofia
    Posts
    40
    Quote Originally Posted by dharma
    Hi,

    Could you please tell me the modules which we have to inherit for working this code.
    Hi,

    well i created my own module, it is not released as jar yet, but it is simple to create your own project as gwt module. Just create java standard project in eclipse, paste the code from above into classes and create .gwt.xml file.
    Here is sample LS.gwt.xml file:
    Code:
    <module>
    
    	<script src="ls/lscommons.js"></script>
    	<script src="ls/lspushpage.js"></script>
    	<inherits name="com.google.gwt.core.Core" />
    	<add-linker name="xs" />
    	<!-- Inherit the core Web Toolkit stuff.                  -->
    	<inherits name='com.google.gwt.user.User' />
    	<inherits name='com.google.gwt.json.JSON' />
    	<inherits name="com.google.gwt.http.HTTP" />
    
    </module>
    It is good to have the ls client js libs in this project, but you may add them in your main project also.

    And remember to include the .java files in the outcoming jar. I am using maven but you may use any build tool.

  4. #4
    Member
    Join Date
    Mar 2010
    Location
    Hyderabad
    Posts
    5
    Thanks For the reply....

    Need one more help....

    I have 3 widgets in the same GWT page. I want to use 3 different Adapters for the Three widgets. Could you please guide me on how to use this ciode.


    Thanks in Advance,
    Dharma.

  5. #5
    Power Member
    Join Date
    Jul 2006
    Location
    Cesano Maderno, Italy
    Posts
    784
    Hi Dharma,

    I don't have an environment to help you developing the extension you need to the GWT related code to fit your needs, and I never had the occasion to play with GWT, but looking at mnechev's code it seems to me quite straightforward.

    Most of the "important" code is in the initialize method where the NonVisualGWTClient uses its values to create a NonVisualTable: you may want to change the code so that a NonVisualGWTClient is decoupled from the table he handle and can create more than a single NonVisualTable.


    Please can you tell me where you got stuck?

  6. #6
    Member
    Join Date
    Apr 2009
    Location
    Tunis
    Posts
    1

    GWT integration step by step

    hi,
    please i like to integrate lightstreamer with gwt, could you give me please a tutoriel step by step that let me to create an example with live grid ( i use eclipse ).*

    thanks

    best regards

  7. #7
    Power Member
    Join Date
    Jul 2006
    Location
    Cesano Maderno, Italy
    Posts
    784
    hello,
    we don't have any step by step tutorial.
    the best resource available is the first post in this thread.
    btw we will release a GWT stocklistdemo in the (near) future, but again, you'll have to dig the sources as there will not be a tutorial.

    can you tell us where did you stuck?

  8. #8
    Power Member
    Join Date
    Jul 2006
    Location
    Cesano Maderno, Italy
    Posts
    784
    hello all,

    just to inform you that we've released a Lightstreamer-GWT integration demo.
    The demo will be soon linked on the demo page of our website, in the meanwhile you can see it working here:
    http://app.lightstreamer.com/GWT_StockListDemo_Basic/

    sources are available form here: http://app.lightstreamer.com/GWT_Sto...ckListDemo.zip

    any feedback is appreciated.

    HTH

 

 

Similar Threads

  1. GWT integration
    By hamroune in forum General
    Replies: 1
    Last Post: January 3rd, 2012, 10:26 AM
  2. Replies: 6
    Last Post: August 10th, 2010, 10:14 AM
  3. Google Web Toolkit
    By sasah in forum Client SDKs
    Replies: 5
    Last Post: August 28th, 2009, 12:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT +1. The time now is 09:03 PM.