1. Help Center
  2. CLI
  3. Developer Guide
  4. SDK
  5. JAVA
  6. ELB Java SDK Demo

ELB Java SDK Demo

Creating a Load Balancer

You can create a load balancer using OpenStack4j based on the following code:

public ELBJob create(LoadBalancerCreate loadBalancer) {
    checkArgument(loadBalancer != null, "loadBalancer is required");     checkArgument(!Strings.isNullOrEmpty(loadBalancer.getName()), "name is required");
    checkArgument(!Strings.isNullOrEmpty(loadBalancer.getVpcId()), "vpcId is required");
    checkArgument(loadBalancer.getType() != null, "type is required");
    checkArgument(loadBalancer.getAdminStateUp() != null, "adminStateUp is required");
    if(Type.INTERNAL.name().equals(loadBalancer.getType())) {                checkArgument(!Strings.isNullOrEmpty(loadBalancer.getVipSubnetId()), "vipSubnetId is required when type is Internal");
        checkArgument(!Strings.isNullOrEmpty(loadBalancer.getAzId()), "azId is required when type is Internal");
        checkArgument(!Strings.isNullOrEmpty(loadBalancer.getTenantId()), "tenantId is required when type is Internal");
    if(Type.EXTERNAL.name().equals(loadBalancer.getType())) {        checkArgument(loadBalancer.getBandwidth() != null, "bandwidth is required when type is External");
    return post(ELBJob.class, uri(API_PATH)).entity(loadBalancer).execute();

Creating a Listener

You can create a listener using OpenStack4j based on the following code. A listener can be created only when a load balancer is available.

public ListenerCreate create(ListenerCreate listener) {
   checkArgument(listener != null, "listener is required");
   checkArgument(!Strings.isNullOrEmpty(listener.getName()), "name is required");
   checkArgument(!Strings.isNullOrEmpty(listener.getLoadBalancerId()), "loadBalancerId is required");
   checkArgument(listener.getProtocol() != null, "protocol is required");
   checkArgument(listener.getPort() != null, "port is required");
   checkArgument(listener.getBackendProtocol() != null, "backendProtocol is required");
   checkArgument(listener.getBackendPort() != null, "backendPort is required");
   checkArgument(listener.getLbAlgorithm() != null, "lbAlgorithm is required");

   return post(ELBListenerCreate.class, uri(API_PATH)).entity(listener).execute();

Performing a Health Check

You can perform a health check using OpenStack4j based on the following code. The health check can be performed only when a listener is available.

public HealthCheck create(HealthCheckCreate healthCheck) {
   checkArgument(healthCheck != null, "healthCheck is reuquired");
   checkArgument(!Strings.isNullOrEmpty(healthCheck.getListenerId()), "listenerId is required");

   return post(ELBHealthCheck.class, uri(API_PATH)).entity(healthCheck).execute();

Adding Members

You can add members to a listener using OpenStack4j based on the following code:

public ELBJob create(String listenerId, List<ServerCreate> servers) {
   checkArgument(!Strings.isNullOrEmpty(listenerId), "listenerId is required");
   checkArgument(servers != null && !servers.isEmpty(), "servers is required");
   for (ServerCreate server : servers) {
      checkArgument(server != null, "server can not be null");
      checkArgument(!Strings.isNullOrEmpty(server.getServerId()), "serverId is required");
      checkArgument(!Strings.isNullOrEmpty(server.getAddress()), "server address is required");

   return post(ELBJob.class, uri("%s/%s/members", API_PATH, listenerId)).entity(servers).execute();

Creating a Certificate

You can create a certificate using OpenStack4j based on the following code:

public Certificate create(Certificate cert) {
   checkArgument(cert != null, "cert is required");
   checkArgument(!Strings.isNullOrEmpty(cert.getCertificate()), "certificate is required");
   checkArgument(!Strings.isNullOrEmpty(cert.getPrivateKey()), "privateKey is required");

   return post(ELBCertificate.class, uri(API_PATH)).entity(cert).execute();