PDA

View Full Version : How to let Data and Metadata Adapters comunicate


CitiMan
12-06-2006, 02:14 PM
How do I get a reference to the Data Adapter (that is being used by the kernel in conjunction with the MetaDataAdapter, not an arbitrary DataAdapter object) instance inside the MetaAdapter?

NOTE by Alessandro: With the release of Lightstreamer Server v.3.5, the current thread has been replaced by this new thread: http://www.lightstreamer.com/vb/showthread.php?t=422

Mone
12-06-2006, 05:49 PM
Hi,

I will expand on this through a little example.
We will put a static HashMap in the MetaDataProvider class, then we will access that HashMap from the DataProvider class, retrieve our related instance and pass to it our DataProvider instance. This flow is needed since LS Kernel initiates the Metadata Adapter before initiating the Data Adapter. This tutorial assumes a basic understanding of Java.

First of all create the adapters.xml file. We will put an additional parameter (match_id) that will help us to make a match between a DataAdapter instance and the MetadataAdapter one. This way, if we have more adapters that share the same class, we just need to give them different match_id to avoid collisions:

<?xml version="1.0"?>

<adapters_conf id="ADAPTER_ID">

<metadata_provider>
<adapter_class>my_package.MyMetadataProvider</adapter_class>
<param name="match_id">ADAPTER_ID</param>
</metadata_provider>

<data_provider>
<adapter_class>my_package.MyDataProvider</adapter_class>
<param name="match_id">ADAPTER_ID</param>
</data_provider>

</adapters_conf>

So MyMetadataProvider will be our MetadataAdapter class.
To implement this class we will extend the classic LiteralBasedProvider. We will reimplement the init method (to put each instance inside a HashMap) and we will add a method for the setting of the related MyDataProvider instance:

package my_package;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.lightstreamer.adapters.metadata.LiteralBasedProvider;
import com.lightstreamer.interfaces.metadata.MetadataProviderException;

public class MyMetadataProvider extends LiteralBasedProvider {

public static HashMap MyDataProviderInstances = new HashMap();

private MyDataAdapter dataAdapter;

public void init(Map params, File configDir) throws MetadataProviderException {
super.init(params,configDir);
String adapter_id = (String) params.get("match_id");
if (adapter_id == null) { throw new MetadataProviderException("match_id for metadata adapter is missing"); }
MyDataProviderInstances.put(adapter_id,this);
}

public void setDataAdapter(MyDataAdapter dataAdapter) {
this.dataAdapter = dataAdapter;
}

}

MyDataProvider will be our DataAdapter class. In the init method of this class we will retrieve the MyMetadataProvider instance from the MyDataProviderInstances HashMap, then we will call its setDataAdapter method. At that point two-way comunication is available. Note that to create a DataProvider implementation you will need to implement a number of methods (like onSubscribe onUnsubscribe etc..) but in this example those are omitted for simplicity:

package my_package;

import java.io.File;
import java.util.Map;

import com.lightstreamer.interfaces.data.DataProvider;
import com.lightstreamer.interfaces.data.DataProviderException;

public class MyDataAdapter implements DataProvider {

private MyMetadataProvider metadataAdapter;

public void init(Map params, File configDir) throws DataProviderException {
String adapter_id = (String) params.get("match_id");
if (adapter_id == null) { throw new DataProviderException("match_id for data adapter is missing"); }
metadataAdapter = (MyMetadataProvider) MyMetadataProvider.MyDataProviderInstances.get(adapter_id);
if (metadataAdapter == null) { throw new DataProviderException("no matching metadata adapter. Check match_id parameters"); }
metadataAdapter.setDataAdapter(this);
}
}

Hope that helps.
Mone.

CitiMan
12-06-2006, 06:25 PM
Forgive me if I misunderstand, but would this design not mean that multiple instances of the same MetaAdapter would overwrite eachother's stored data provider instance through MyDataProviderInstances.put(adapter_id,this)?

I need to communicate with the DataAdapter instance that provides the data to the particular MetaDataAdapter in question. Although all clients will be using the same DataAdapter, they will pass in different parameters through sendMessage that will cause each DataAdapter instance to send data that will differ based on this. I'm not sure that is possible with what you have suggested?

Mone
12-07-2006, 07:57 PM
Each Adapter is made with 2 object's instances. A DataProvider instance and a MetadataProvider one.
To configure an Adapter you must create an adapters.xml file and put it under the adapters folder (refer to LS_HOME/DOCS-SDKs/sdk_adapter_java/examples/StockListDemo/Deployment_LS/README.TXT. for further details on how to deploy an adapter) In this file you choose the DataProvider class, the MetadaProvider class and it's also possible to set parameters to be passed to the class's instances.

Obviously you can use a class to create more Adapters. eg you have 2 adapters. Just set the 2 adapters.xml as follows:
The first one will use MyDataProvider as DataProvider and MyMetadataProvider as MetadataProvider.

<?xml version="1.0"?>

<adapters_conf id="ADAPTER_1">
<metadata_provider>
<adapter_class>my_package.MyMetadataProvider</adapter_class>
<param name="match_id">ADAPTER_1</param>
</metadata_provider>

<data_provider>
<adapter_class>my_package.MyDataProvider</adapter_class>
<param name="match_id">ADAPTER_1</param>
</data_provider>
</adapters_conf>


The other use MyDataProvider2 as DataProvider and MyMetadataProvider as MetadataProvider.

<?xml version="1.0"?>

<adapters_conf id="ADAPTER_2">
<metadata_provider>
<adapter_class>my_package.MyMetadataProvider</adapter_class>
<param name="match_id">ADAPTER_2</param>
</metadata_provider>

<data_provider>
<adapter_class>my_package.MyDataProvider2</adapter_class>
<param name="match_id">ADAPTER_2</param>
</data_provider>
</adapters_conf>


At this point you have 2 MyMetadaProvider instances and each one receives its match_id parameter.

String adapter_id = (String) params.get("match_id");

so adapter_id is "ADAPTER_1" for the first instance and "ADAPTER_2" for the other.

Now with this call

MyDataProviderInstances.put(adapter_id,this);

each MyMetadataProvider instance puts in the MyDataProviderInstances HashMap its reference with a different key (equals to the match_id received).

From the other side MyDataProvider an MyDataProvider2 instances get each one a different MyMetadataProvider instance.

String adapter_id = (String) params.get("match_id");


metadataAdapter = (MyMetadataProvider) MyMetadataProvider.MyDataProviderInstances.get(adapter_id);


Mone.

PS: I saw that on my previous post i fogot to instantiate MyDataProviderInstances. Sorry.

Alessandro
11-21-2008, 06:24 PM
With the release of Lightstreamer Server v.3.5, the current thread has been replaced by this new thread: http://www.lightstreamer.com/vb/showthread.php?t=422