-
December 6th, 2006, 01:14 PM
#1
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
-
December 6th, 2006, 04:49 PM
#2
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:
Code xml:
<?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:
Code java:
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 {
private MyDataAdapter dataAdapter;
public void init
(Map params,
File configDir
) throws MetadataProviderException
{
super.init(params,configDir);
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:
Code java:
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
{
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.
-
December 6th, 2006, 05:25 PM
#3
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?
-
December 7th, 2006, 06:57 PM
#4
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.
Code xml:
<?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.
Code xml:
<?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.
so adapter_id is "ADAPTER_1" for the first instance and "ADAPTER_2" for the other.
Now with this call
Code java:
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.
Code java:
metadataAdapter = (MyMetadataProvider) MyMetadataProvider.MyDataProviderInstances.get(adapter_id);
Mone.
PS: I saw that on my previous post i fogot to instantiate MyDataProviderInstances. Sorry.
-
November 21st, 2008, 05:24 PM
#5
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
Similar Threads
-
By odeheurles in forum Adapter SDKs
Replies: 6
Last Post: January 26th, 2015, 09:50 AM
-
By vnipt in forum General
Replies: 3
Last Post: November 14th, 2011, 09:33 AM
-
By emese in forum Adapter SDKs
Replies: 1
Last Post: July 21st, 2011, 10:16 AM
-
By rd2008 in forum Client SDKs
Replies: 2
Last Post: January 7th, 2009, 02:28 PM
-
By chinghunglai in forum General
Replies: 2
Last Post: March 26th, 2008, 09:50 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
All times are GMT +1. The time now is 06:49 PM.
Bookmarks