How to Use Alibaba Cloud LOG Java Producer


The Alibaba Cloud LOG Java Producer is high performance write LogHub library that is designed for Java applications running in big data and high concurrency scenarios. In comparison with using APIs or SDKs, using Alibaba Cloud LOG Java Producer (Producer) has many advantages, such as high performance, isolated computing and I/O logic, and controllable resource usage. To understand the features and mechanisms of the Producer, see the article Alibaba Cloud LOG Java Producer — A powerful tool to migrate logs to the cloud. This article will focus on the use of the Producer.


You can perform three steps to use the Producer as shown in the following figure.

Create Producer

The following objects are involved when you create the Producer.


The ProjectConfig object contains the service endpoint information of the target project, and the access credential that indicates the identity of the caller.

Service Endpoint

The final access address is composed of the project name and the service endpoint. For more information about how to determine the endpoint of a project, see Service endpoint.

Access Credential

You can configure the AccessKey or Security Token Service (STS) token for the Producer. If you use the STS token, you must regularly create new ProjectConfig objects, and put them into ProjectConfigs.


If you need to write data to different projects, you can create multiple ProjectConfig objects, and put them to ProjectConfigs. ProjectConfigs maintains different project configurations through a map. The key of the map is the project name, and the value is the client of the project.


ProducerConfig is used to configure the sending policy. You can specify different values according to different business scenarios. The descriptions of these parameters are provided in the following table.


LogProducer is the implementation class of Producer and it only accepts the producerConfig parameter. After you have prepared the producerConfig, you can create a Producer instance as follows:

Producer producer = new LogProducer(producerConfig);

Send Data

After creating a Producer instance, you can use methods it provides to send data.

Parameter description

The Producer provides multiple data sending methods. Parameters of these methods are described as follows.

Obtain the Data-Sending Result

The Producer sends data asynchronously. You need to obtain the data-sending result from future objects or callbacks returned by the Producer.


The send method returns a ListenableFuture. The ListenableFuture not only allows you to block the I/O thread and obtain the data-sending result by calling the get() method, but also allows you to register a callback. The callback will be called after the future settings are complete. The following snippet shows you how to use ListenableFuture. You need to register a FutureCallback for this future, and send the callback to the EXECUTOR_SERVICE thread pool that is provided by the application for execution. For complete example code, see

ListenableFuture<Result> f = producer.send("project", "logStore", logItem);
new FutureCallback<Result>() {
public void onSuccess(@Nullable Result result) {
public void onFailure(Throwable t) {


In addition to future, you can also register a callback when you call the send method to obtain the data-sending result. The code snippet is as follows. For complete example code, see

new Callback() {
public void onCompletion(Result result) {

Comparison between Future and Callback

Which should I choose to obtain the data-sending result, future or callback? If the processing logic after you obtain the result is relatively simple and does not block the I/O thread of the Producer, directly use callback. Otherwise, we recommend that you use ListenableFuture to run the subsequent processing logic in a separate thread (pool).

Shut down the Producer

If you do not have more data to be sent, or if you are going to exit the current process, you need to shut down the Producer. By doing so, the cached data of the Producer can be fully processed. Currently, the Producer supports safe shutdown and limited shutdown.

Safe Shutdown

In most cases, we recommend that you use safe shutdown. You can call the close() method to safely shut down the Producer. This method returns only when all cached data of the Producer is processed, all threads are terminated, the registered callbacks are executed, and all futures have been set.

Limited Shutdown

If your callback is likely to be blocked when it is executed, and you want the close method to return shortly, use the limited shutdown mode. You can use the close(long timeoutMs) method to implement the limited shutdown. If the Producer is not completely shut down after the expiration of the specified timeoutMs, it throws an IllegalStateException exception. In this case, the Producer is shut down regardless of whether the cached data has been processed or whether the registered callbacks have been executed.

Sample Application

To make learning Producer easier, we have prepared the Alibaba Cloud LOG Java Producer Sample Application for you. The sample covers the entire process from Producer creation to shutdown.

Original Source



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website: