Skip Navigation

Call TypeScript service from Java

As Red App developer you can call TypeScript services from Java backend and receive a response.

Create TypeScript service

In TypeScript, you can create new service by extending AbstractService class.

You have to add SERVICE_NAME that starts with web-module name (com-sabre-redapp-example3-desktop-serviceregistry-web-module in the example)

export class EchoService extends AbstractService {
  static SERVICE_NAME =
    'com-sabre-redapp-example3-desktop-serviceregistry-web-module-echo-service'; // (1)

  echo(message: string) { // (2)
    //...
  }
}
  1. Set service name

  2. Method that will be called from Java

Next step is to register created service in the Main.ts:

//...
import { registerService } from './Context';
import { EchoService } from './service/EchoService'; (1)

export class Main extends Module {
  init(): void {
    //...

    registerService(EchoService); (2)
  }

  //...
}
  1. Import created service

  2. Register service

In the last step, service has to be declared in src/manifest.json file in serviceRegistry field, under services array.

{
  "name": "com-sabre-redapp-example3-desktop-serviceregistry-web-module",
  "serviceRegistry": {
    "services": [
      {
        "name": "com-sabre-redapp-example3-desktop-serviceregistry-web-module-echo-service", // (1)
        "impl": "EchoService" // (2)
      }
    ]
  }
}
  1. Service registry name

  2. Service implementation class name

Call TypeScript service from Java

TypeScript service can be called from Java backend using RegistryServiceClient and corresponding RegistryServiceRequest where service and method names can be set as well as parameters for given method.

Response from TypeScript is kept as String values - so primitives will be "stringified" and objects will be returned as JSON string.

Errors can be accessed via getErrors method on response.

Example usage:

ISRWCommunication com = Activator.getDefault().getServiceReference(ISRWCommunication.class); // (1)

public void execute()
{
    String message = "Hello from Java";
    RegistryServiceClient client = new RegistryServiceClient(com); // (2)
    RegistryServiceRequest request = new RegistryServiceRequest( // (3)
        "com-sabre-redapp-example3-desktop-serviceregistry-web-module-echo-service",
        "echo",
        new String[]{message});

    ClientResponse <RegistryServiceResponse> resp =
        (ClientResponse <RegistryServiceResponse>) client.send(request); // (4)

    if (resp.getErrors().isEmpty()) // (5)
    {
        System.out.println(resp.getPayload().getReturnedValue());
    }
    else
    {
        resp.getErrors().stream().map(error -> error.getDescription())
            .forEach(message -> System.out.println(message));
    }
}
  1. Initialize SRW communication bus interface

  2. Create RegistryServiceClient instance

  3. Create RegistryServiceRequest instance with the following parameters:

    • service registry name of TypeScript service to be called

    • method to be called

    • params for called method

  4. Send request

  5. Handle response and potential errors

Sample implementation

Working sample can be found in samples directory under com.sabre.redapp.example3.desktop.serviceregistry name.