-
JDBC Data Adapter [Java]
Below you can find a very simple example source code for a Java Data Adapter. We modified the HelloWorldDataAdapter to poll a database, instead of generating the data internally.
This Data Adapter uses JDBC to query a MySQL table every second. It simply reads the first record, extracts the "message" and "timestamp" fields and injects them into the Lightstreamer Kernel.
-
Hi alessandro,
If i want to write the adapters.xml file for this project how will it be like? Can u provide a reference for it? Will i be needing <metadataprovider> for this code as metadataprovider is not used
-
Hi Vinayak Singh,
For such a simple example like HelloWorld, the configuration file of the AdapterSet is equally simple, and you can find it described here: https://github.com/Lightstreamer/Lig...-configuration
In fact, the adapters.xml file can be completely identical to the one from the Java project from which the above extension is derived.
But please let me to add two considerations.
The first one is that one (and only one) Metadata Adapter is mandatory for each Adapter Set. In fact, every client request, whether it's for opening a client session, subscribing or unsubscribing to items, or sending messages, is subject to authentication and/or authorization by the Metadata Adapter.
For this reason, in order to make examples like this one simpler, we have developed a simple full implementation of Metadata Adapter in Java, LiteralBasedProvider, made available as sample for inspiration and/or extension and enbedded in the Lightstreamer Java Adapter libraries.
Please refer here for more details: https://github.com/Lightstreamer/Lig...tadata-adapter
The second one is that since this example adds a connection to the database from which to retrieve messages to send to clients, it would be a natural extension to add configurations to avoid hardcoding all the parameters of the database connection string in the code.
For example you could add to the <data_provider> section something like these:
<param name="db_hostname">localhost</param>
<param name="db_user">root</param>
<param name="db_password">xxx</param>
<param name="jdbc_driver">com.mysql.jdbc.Driver</param>
And any other parameter that you deem necessary, such as table names or column names, etc.
Regards,
Giuseppe
-
Hi Giuseppe,
Thank you for the reply, I just wanted to confirm with the adapters.xml file that i wrote and want you to check it for this above code.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Do not remove this line. File tag: adapters_conf-APV-7.2.0. -->
<!-- This is a generic template for the configuration file of an Adapter Set pluggable into Lightstreamer Server. It can be considered a reference example of an in-process Java Adapter deploy. Note that element and attribute names are case-sensitive.
A very simple variable-expansion feature is available; see <enable_expansion_for_adapters_config> in the Server's main configuration file. -->
<!-- Mandatory. Define an Adapter Set and its unique ID. -->
<adapters_conf id="HELLOWORLD">
<metadata_provider>
<!-- Mandatory. Java class name of the adapter. -->
<adapter_class>classfile.adapters.metadata.Literal BasedProvider</adapter_class>
</metadata_provider>
<!-- Mandatory. Define a Data Adapter named "HelloWorldAdapter". -->
<data_provider name="HelloWorldDataAdapter">
<param name="db_hostname">localhost</param>
<param name="db_user">root</param>
<param name="db_password">xxxx</param>
<param name="jdbc_driver">com.mysql.jdbc.Driver</param>
<adapter_class>classfile.adapters.HelloWorldDataAd apter</adapter_class>
</data_provider>
</adapters_conf>
If this is correct i am going to paste the adapters folder in the lightstreamer server folder. And then how to see the output in the lightstreamer server?
-
Certainly, since you have added parameters in the `<data_provider>` section, you will need to add code to read and use them. Therefore, in the `init` method of your Data Adapter, you will need to implement something similar to the following:
```java
public void init(Map params, File configDir) throws DataProviderException {
// Read the DB parameters
String db_host = (String) params.get("db_hostname");
String db_user = (String) params.get("db_user");
...
}
```
Once you have retrieved the configuration parameters, you can utilize them within the `init` method or store them as instance variables to be used in other methods of your Data Adapter.
Sorry but I am not sure what do you mean for "output in the Lightstreamer", if you refer to the log messages generated by Lightstreamer, including the logs related to the loading of adapters and client requests. You can find these logs in the <LS_HOME>/logs directory.
If you are looking for a web application that acts as a client for this adapter, you can refer to this project:
[Project Name: Lightstreamer - "Hello World" Tutorial - HTML Client]
[GitHub Repository: https://github.com/Lightstreamer/Lig...ent-javascript]
This web application can serve as a client for your adapter and provide an interface for the visualization of the data provided by the adapter. You can find more details and the necessary code in the provided GitHub repository.
Reagrds,
Giuseppe
-
Hi Giuseppe,
I am encountering an error while trying to run this code in the IntelliJ. The error is
No com.lightstreamer.kernel_lib_path defined; using a unique ClassLoader; ignoring any other _lib_path supplied
Unexpected error in initialization phase:
How to solve this error? I am using a windows system. Please help me regarding this issue, This is my adapters.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!-- Do not remove this line. File tag: adapters_conf-APV-7.2.0. -->
<!-- This is a generic template for the configuration file of an Adapter Set pluggable into Lightstreamer Server. It can be considered a reference example of an in-process Java Adapter deploy. Note that element and attribute names are case-sensitive.
A very simple variable-expansion feature is available; see <enable_expansion_for_adapters_config> in the Server's main configuration file. -->
<!-- Mandatory. Define an Adapter Set and its unique ID. -->
<adapters_conf id="HELLOWORLD">
<metadata_provider>
<!-- Mandatory. Java class name of the adapter. -->
<adapter_class>classfile.adapters.metadata.Literal BasedProvider</adapter_class>
</metadata_provider>
<!-- Mandatory. Define a Data Adapter named "HelloWorldAdapter". -->
<data_provider name="HelloWorldDataAdapter">
<param name="db_hostname">localhost</param>
<param name="db_user">root</param>
<param name="db_password">xxxx</param>
<param name="jdbc_driver">com.mysql.jdbc.Driver</param>
<adapter_class>classfile.adapters.HelloWorldDataAd apter</adapter_class>
</data_provider>
</adapters_conf>
-
The "Unexpected error in initialization phase:" should be followed by the actual error message.
Do you see anything after that line?
The previous message, "No com.lightstreamer.kernel_lib_path defined; using a unique ClassLoader; ignoring any other _lib_path supplied", is not an error.
It can be issued if you launch the Server directly, without using the factory scripts and without passing the needed JVM properties, but this does not prevent the Server from starting.
However, if there is something missing or wrong in the classpath supplied to the JVM, this may explain the startup failure.
But to understand what's wrong we need either to see the full log, or to see exactly the command you used to launch the JVM.
-
Hi Dario,
With continuation to my last message i want to add the other related errors i got while executing the code
java.lang.NoClassDefFoundError: ch/qos/logback/core/Context
at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
at java.base/java.lang.Class.privateGetDeclaredConstructors(Cla ss.java:3373)
at java.base/java.lang.Class.getConstructor0(Class.java:3578)
at java.base/java.lang.Class.getConstructor(Class.java:2271)
at com.lightstreamer.init.p.a(p.java)
at com.lightstreamer.init.o.a(o.java)
at com.lightstreamer.init.o.a(o.java)
at com.lightstreamer.f.e.b.a(b.java)
at com.lightstreamer.b.b.a(b.java)
at com.lightstreamer.b.b.a(b.java)
at com.lightstreamer.b.a.a(a.java)
at com.lightstreamer.LS.main(LS.java)
Caused by: java.lang.ClassNotFoundException: ch.qos.logback.core.Context
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(B uiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.lo adClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:5 20)
... 12 more
Initialization error.
Startup failed.
Please help me solve the error!
-
Hi, Also in this thread, I can't but refer to this other thread of yours and in particular to post #4.
To resume, if you open a command prompt from the "Lightstreamer" folder of the Server installation package, you should run a command like this:
Code:
java -cp "lib/*;lib/log/*;lib/core/*;lib/adapters/*;lib/proxy/*" com.lightstreamer.LS conf\lightstreamer_conf.xml
This is the simplest way to run the Server. It will log "No com.lightstreamer.kernel_lib_path defined; using a unique ClassLoader; ignoring any other _lib_path supplied" at startup, but this should not prevent it from working.
Launched in this way, there may be problems only in case of conflicts between third-party libraries used by the Server and libraries used by your Adapters, but they can arise only after the initial startup.
In particular, the classes from Logback that you can't load are in the "lib/log/*" part of the classpath shown.
Please try to do exactly the same and confirm if it works.
Then, use that as the baseline that you should refer to when setting up the launch in you own context.
-
Hi Dario,
Can u list the properties and the jar files needed for the JVM platform setup to start the lightstreamer. Also include the paths as well.
Thank you!
-
Hi Dario,
I solved the previous error and the lightstreamer server almost started but while configuring the adapters of this project it went wrong. I am sharing the logs for you
22.May.23 17:34:57,058 < WARN> Adapters configuration not found in C:\Users\SinghVinayak\Lightstreamer-7.3.3\Lightstreamer\conf\..\adapters\classfile
22.May.23 17:34:57,075 < INFO> data_provider element without name attribute; using DEFAULT as the default name.
22.May.23 17:34:57,109 < INFO> Loading Metadata Adapter for Adapter Set WELCOME
22.May.23 17:34:57,109 < INFO> Loading Metadata Adapter for Adapter Set HELLOWORLD
22.May.23 17:34:57,799 < INFO> Finished loading Metadata Adapter for Adapter Set HELLOWORLD
22.May.23 17:34:57,799 < INFO> Loading Data Adapter HELLOWORLD.DEFAULT
22.May.23 17:34:57,848 < INFO> Finished loading Data Adapter HELLOWORLD.DEFAULT
22.May.23 17:34:58,213 < INFO> Finished loading Metadata Adapter for Adapter Set WELCOME
22.May.23 17:34:58,214 < INFO> Loading Data Adapter WELCOME.TELEMETRY
22.May.23 17:34:58,214 < INFO> Loading Data Adapter WELCOME.CHAT
22.May.23 17:34:58,214 < INFO> Loading Data Adapter WELCOME.STOCKS
22.May.23 17:34:58,214 < INFO> Loading Data Adapter WELCOME.ROOMBALL
22.May.23 17:34:58,216 < INFO> Finished loading Data Adapter WELCOME.TELEMETRY
22.May.23 17:34:58,217 < INFO> Finished loading Data Adapter WELCOME.ROOMBALL
22.May.23 17:34:58,223 < INFO> Finished loading Data Adapter WELCOME.CHAT
22.May.23 17:34:58,223 < INFO> Finished loading Data Adapter WELCOME.STOCKS
22.May.23 17:34:58,228 < INFO> Events pool size set by default at 8.
22.May.23 17:34:58,230 < INFO> Snapshot pool size set by default at 10.
22.May.23 17:34:58,234 <ERROR> Unexpected error in configuration: java.lang.RuntimeException: java.lang.NoClassDefFoundError: org/bitbucket/cowwoc/diffmatchpatch/DiffMatchPatch.
22.May.23 17:34:58,235 <ERROR> Error while loading the Adapters: Unexpected error in configuration.
22.May.23 17:34:58,235 <FATAL> Adapters initialization failed.
Process finished with exit code -1
-
Hi Dario,
I solved the error that i listed first now in the logs there is a error for the server initialization. It says-
22.May.23 17:48:31,379 < WARN> Lightstreamer Server is running with a Demo license, which has a limit of 20 concurrent users and can be used for evaluation, development, and testing, but not for production. If you need to evaluate Lightstreamer Server without this user limit, or need any information on the other license types, please contact info@lightstreamer.com
22.May.23 17:48:31,485 < INFO> Number of detected cores: 8
22.May.23 17:48:31,486 < INFO> Lightstreamer Server starting in ENTERPRISE edition.
22.May.23 17:48:31,606 < INFO> Started RMI server for JNDI for JMX Connector on port 8888
22.May.23 17:48:31,614 < INFO> Binding of RMI Connector for JMX on port 8888 (communication on port 8888) started...
22.May.23 17:48:31,662 < INFO> Binding of RMI Connector for JMX on port 8888 (communication on port 8888) successful.
22.May.23 17:48:31,663 < INFO> Test of reachability of the JMX Connector at localhost:8888 started...
22.May.23 17:48:31,680 < INFO> Test of reachability of the JMX Connector at localhost:8888 successful.
22.May.23 17:48:31,737 < INFO> Binding of RMI Connector for Platform mbeans on port 8888 (communication on port 8888) started...
22.May.23 17:48:31,740 < INFO> Binding of RMI Connector for Platform mbeans on port 8888 (communication on port 8888) successful.
22.May.23 17:48:31,744 < INFO> SERVER pool size set by default at 1000.
22.May.23 17:48:31,745 < INFO> ACCEPT pool size set by default at 8.
22.May.23 17:48:31,771 < INFO> Queue limit checks for SERVER pool added to checks for ACCEPT pool.
22.May.23 17:48:31,813 < WARN> Adapters configuration not found in C:\Users\SinghVinayak\Downloads\Lightstreamer-7.3.3\Lightstreamer\conf\..\adapters\classfile
22.May.23 17:48:31,817 < INFO> data_provider element without name attribute; using DEFAULT as the default name.
22.May.23 17:48:31,834 < INFO> Loading Metadata Adapter for Adapter Set HELLOWORLD
22.May.23 17:48:31,834 < INFO> Loading Metadata Adapter for Adapter Set WELCOME
22.May.23 17:48:31,837 < INFO> Finished loading Metadata Adapter for Adapter Set HELLOWORLD
22.May.23 17:48:31,848 < INFO> Loading Data Adapter HELLOWORLD.DEFAULT
22.May.23 17:48:31,850 < INFO> Finished loading Data Adapter HELLOWORLD.DEFAULT
22.May.23 17:48:32,171 < INFO> Finished loading Metadata Adapter for Adapter Set WELCOME
22.May.23 17:48:32,171 < INFO> Loading Data Adapter WELCOME.TELEMETRY
22.May.23 17:48:32,171 < INFO> Loading Data Adapter WELCOME.CHAT
22.May.23 17:48:32,171 < INFO> Loading Data Adapter WELCOME.STOCKS
22.May.23 17:48:32,171 < INFO> Loading Data Adapter WELCOME.ROOMBALL
22.May.23 17:48:32,173 < INFO> Finished loading Data Adapter WELCOME.TELEMETRY
22.May.23 17:48:32,174 < INFO> Finished loading Data Adapter WELCOME.ROOMBALL
22.May.23 17:48:32,178 < INFO> Finished loading Data Adapter WELCOME.STOCKS
22.May.23 17:48:32,178 < INFO> Finished loading Data Adapter WELCOME.CHAT
22.May.23 17:48:32,183 < INFO> Events pool size set by default at 8.
22.May.23 17:48:32,186 < INFO> Snapshot pool size set by default at 10.
22.May.23 17:48:32,190 <ERROR> Unexpected error in configuration: java.lang.RuntimeException: java.lang.NoClassDefFoundError: org/bitbucket/cowwoc/diffmatchpatch/DiffMatchPatch.
22.May.23 17:48:32,191 <ERROR> Error while loading the Adapters: Unexpected error in configuration.
22.May.23 17:48:32,191 <FATAL> Adapters initialization failed.
Process finished with exit code -1
-
The org/bitbucket/cowwoc/diffmatchpatch/DiffMatchPatch class is provided by the diff-match-patch-1.2.jar library in the Lightstreamer\lib\core folder.
Please check if you can see it.
If you can't, perhaps you have updated the Server binaries to version 7.3 in an installation package for version 7.2?
To resume the full list of libraries needed for the Server 7.3 startup, a procedural description seems better:
- Download Server version 7.3 from the download page.
- Go to the Lightstreamer/lib folder.
- Consider all jar files in this folder and all its subfolders, except for the jars included in the log\bridge subfolder.