> For the complete documentation index, see [llms.txt](https://developer.barchart.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.barchart.com/repos-sdks/java-sdk.md).

# Java SDK

### Use Cases for Java Software Development Kit

The **Java SDK for the Streaming Openfeed API** provides a high-performance implementation for consuming real-time market data through the Openfeed protocol. Designed for institutional-grade applications, the Java SDK is particularly well-suited for **full-pipe exchange subscriptions**, where large volumes of instrument definitions, metadata, and real-time updates are transmitted in rapid succession.

Java operates closer to the system level. Its performance characteristics and efficient memory management allow applications to process high-throughput packet streams reliably, making it an ideal language for building production-grade **feed handlers, data distribution services, and internal market data pipelines**. Whereas higher-level programming languages work fine for symbol-limited subscriptions, but they are not well-suited for full-pipe exchange feeds.

### Github Repository

To download, sync, and install Java SDK for Barchart's Streaming API, you can visit our Github repo below.

{% embed url="<https://github.com/openfeed-org/sdk-java>" %}

### Connecting

The Openfeed SDK's will establish a secure Websocket (wss) connection to the Streaming Openfeed edge servers. By default, you will connect to

Additionally, each SDK will also authenticate your application and provide interfaces to subscribe to market data streams.

{% code expandable="true" %}

```java
package org.openfeed.client.examples;

import org.openfeed.ExchangeResponse;
import org.openfeed.HeartBeat;
import org.openfeed.InstrumentAction;
import org.openfeed.InstrumentDefinition;
import org.openfeed.InstrumentReferenceResponse;
import org.openfeed.InstrumentResponse;
import org.openfeed.LoginResponse;
import org.openfeed.LogoutResponse;
import org.openfeed.MarketSnapshot;
import org.openfeed.MarketStatus;
import org.openfeed.MarketUpdate;
import org.openfeed.Ohlc;
import org.openfeed.Service;
import org.openfeed.SubscriptionResponse;
import org.openfeed.SubscriptionType;
import org.openfeed.VolumeAtPrice;
import org.openfeed.client.api.OpenfeedClient;
import org.openfeed.client.api.OpenfeedClientEventHandler;
import org.openfeed.client.api.OpenfeedClientHandler;
import org.openfeed.client.api.OpenfeedEvent;
import org.openfeed.client.api.impl.ConnectionStats;
import org.openfeed.client.api.impl.OpenfeedClientConfigImpl;
import org.openfeed.client.api.impl.websocket.OpenfeedClientWebSocket;

import io.netty.channel.ChannelPromise;

public class Example implements OpenfeedClientEventHandler, OpenfeedClientHandler {

    OpenfeedClientWebSocket client;

    public static void main(String[] args) {
        new Example();
    }

    public Example() {

        final OpenfeedClientConfigImpl config = new OpenfeedClientConfigImpl();

        config.setUserName("username");
        config.setPassword("password");

        client = new OpenfeedClientWebSocket(config, this, this);

        client.connectAndLogin();
    }

    @Override
    public void onLoginResponse(LoginResponse loginResponse) {
        System.out.println("onLoginResponse: " + loginResponse);

        /* Subscribe to OHLC for Apple */
        client.subscribe(Service.REAL_TIME, SubscriptionType.OHLC, new String[] { "AAPL" });

        /* Some time later, Subscribe to front month Corn */
        new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                }
                
                /* OHLC for front-month Corn */
                client.subscribe(Service.REAL_TIME, SubscriptionType.OHLC, new String[] { "ZC*0" });
            }
        }.start();
    }

    @Override
    public void onLogoutResponse(LogoutResponse logoutResponse) {
        System.out.println("onLogoutResponse: " + logoutResponse);
    }

    @Override
    public void onInstrumentResponse(InstrumentResponse instrumentResponse) {
        System.out.println("onInstrumentResponse: " + instrumentResponse);
    }

    @Override
    public void onInstrumentReferenceResponse(InstrumentReferenceResponse instrumentReferenceResponse) {
        System.out.println("onInstrumentReferenceResponse: " + instrumentReferenceResponse);
    }

    @Override
    public void setInstrumentPromise(ChannelPromise promise) {
        System.out.println("setInstrumentPromise: " + promise);
    }

    @Override
    public void onExchangeResponse(ExchangeResponse exchangeResponse) {
        System.out.println("onExchangeResponse: " + exchangeResponse);
    }

    @Override
    public void onSubscriptionResponse(SubscriptionResponse subscriptionResponse) {
        System.out.println("onSubscriptionResponse: " + subscriptionResponse);
    }

    @Override
    public void onMarketStatus(MarketStatus marketStatus) {
        System.out.println("onMarketStatus: " + marketStatus);
    }

    @Override
    public void onHeartBeat(HeartBeat hb) {
        System.out.println("onHeartBeat: " + hb);
    }

    @Override
    public void onInstrumentDefinition(InstrumentDefinition definition) {
        System.out.println("onInstrumentDefinition: " + definition);
    }

    @Override
    public void onMarketSnapshot(MarketSnapshot snapshot) {
        System.out.println("onMarketSnapshot: " + snapshot);
    }

    @Override
    public void onMarketUpdate(MarketUpdate update) {
        System.out.println("onMarketUpdate: " + update);
    }

    @Override
    public void onVolumeAtPrice(VolumeAtPrice cumulativeVolume) {
        System.out.println("onVolumeAtPrice: " + cumulativeVolume);
    }

    @Override
    public void onOhlc(Ohlc ohlc) {
        System.out.println("onOhlc: " + ohlc);
    }

    @Override
    public void onInstrumentAction(InstrumentAction instrumentAction) {
        System.out.println("onInstrumentAction: " + instrumentAction);
    }

    @Override
    public ConnectionStats getConnectionStats() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onEvent(OpenfeedClient client, OpenfeedEvent event) {
        System.out.println("onEvent: " + event);
    }

}

```

{% endcode %}

### Streaming by Symbol

You can stream by symbol or a list of symbols. Below is an example using the SDK client.

{% code expandable="true" %}

```java
final OpenfeedClientWebSocket client = new OpenfeedClientWebSocket(config, eventHandler, clientHandler);

client.subscribe(Service.REAL_TIME, SubscriptionType.OHLC, new String[] {"MSFT", "TSLA"});

```

{% endcode %}

### Subscribing by Exchange

You can stream by the full exchange feed. You must request access from Barchart's Institutional Support Team as this subscription type requires configuration changes to your authentication credentials. Please contact support for more details.

{% code expandable="true" %}

```java
client.subscribeExchange(Service.REAL_TIME, SubscriptionType.OHLC, new String[] { "NYSE" });

```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developer.barchart.com/repos-sdks/java-sdk.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
