MT4 manager gRPC client Java example

Created with Sketch.

MT4 manager gRPC client Java example

Table of Contents

API methods browser

MT4 manager proto file

Install before run an example

Do maven clean installation with extended debug info.
It generates libraries with stubs we need for method invocation.

mvn clean install -X

Before:

After. Clases were generated:

If you need generated classes separately from the example project, you can download it from repository

Input your credential information

Now we are ready to run.

Ready to run example

The example:

import io.grpc.ManagedChannelBuilder;

public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );

        String host = "mng4grpc.mtapi.io";
        int port = 443;
        ManagedChannelBuilder<?> channelBuilder = ManagedChannelBuilder.forAddress(host, port);
        MT4mngGrpcApiClient client = new MT4mngGrpcApiClient(channelBuilder.build());
        client.Connect();
    }
}
import io.grpc.Channel;
import io.grpc.StatusRuntimeException;
import mng4grpc.MainControllerGrpc;
import mng4grpc.Mt4Mng;

import java.util.List;

public class MT4mngGrpcApiClient {

    private final MainControllerGrpc.MainControllerBlockingStub mainControllerBlockingStub;
    private String token;
    public void setToken(String token) {
        this.token = token;
    }

    public MT4mngGrpcApiClient(Channel channel) {
        mainControllerBlockingStub = MainControllerGrpc.newBlockingStub(channel);
    }

    public void Connect()
    {
        Mt4Mng.ConnectRequest connectRequest = Mt4Mng.ConnectRequest.newBuilder()
                .setServer("")
                .setUser(1)
                .setPassword("")
                .build();
        try
        {
            Mt4Mng.ConnectReply connectReply = mainControllerBlockingStub.connect(connectRequest);
            if (connectReply.getError().getCodeValue() != 0)
            {
                System.out.println("Connection failed: ".concat(connectReply.getError().getMessage()));
            }
            System.out.println("Connection response id: ".concat(connectReply.getResult()));
            setToken(connectReply.getResult());

        }
        catch (StatusRuntimeException e){
            System.out.println("RPC failed:".concat(e.getStatus().toString()));
            return;
        }

        try
        {
            Mt4Mng.AccountsReply accountsReply = mainControllerBlockingStub.accountsList(
                    Mt4Mng.AccountsRequest.newBuilder()
                            .setId(token)
                            .build());
            if (accountsReply.getError().getCodeValue() != 0)
            {
                System.out.println("Connection failed: ".concat(accountsReply.getError().getMessage()));
            }
            List<Integer> accounts = accountsReply.getResultList();
            for (Integer accountId : accounts) {
                System.out.println("Account Is: ".concat(String.valueOf(accountId)));
                Mt4Mng.AccountDetailsReply accountReply = mainControllerBlockingStub.accountDetails(Mt4Mng.AccountDetailsRequest.newBuilder()
                        .setId(token)
                        .setLogin(accountId)
                        .build());
                if (accountReply.getError().getCodeValue() != 0)
                {
                    System.out.println("Connection failed: ".concat(accountReply.getError().getMessage()));
                }
                System.out.println("Account City: ".concat(accountReply.getResult().getCity()));
                System.out.println("Account Balance: ".concat(String.valueOf(accountReply.getResult().getBalance())));
            }
        }
        catch (StatusRuntimeException e){
            System.out.println("RPC failed:".concat(e.getStatus().toString()));
        }
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *