Skip to content

Instantly share code, notes, and snippets.

@RichardHightower
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardHightower/4bb3921edec2e663b955 to your computer and use it in GitHub Desktop.
Save RichardHightower/4bb3921edec2e663b955 to your computer and use it in GitHub Desktop.
example changing the uri to MRS

Here is an example of how to change the root address from /services to something else. And this is also an example of working with the Docker, Mesos, Heroku friendly version of QBit.

package qbit.todo;

import io.advantageous.qbit.admin.ManagedServiceBuilder;

import io.advantageous.qbit.server.ServiceEndpointServer;


public class TodoRestServer {



    public static void main(String... args) {


        final ManagedServiceBuilder managedServiceBuilder = ManagedServiceBuilder.managedServiceBuilder();

        final ServiceEndpointServer serviceServer = managedServiceBuilder.getEndpointServerBuilder().setUri("/mrs")
                .build()
                .initServices(new TodoService()).startServer();

        /* Wait for the service to shutdown. */
        managedServiceBuilder.getSystemManager().waitForShutdown();

    }




}
...
package qbit.todo;

import io.advantageous.qbit.annotation.*;


/**
 * Created by rhightower on 2/10/15.
 */
@RequestMapping("/todoservice")
public class TodoService {


    @RequestMapping("/todoo")
    public Todo list() {
        return new Todo("Hello");
    }

}
...
package qbit.todo;

public class Todo {
    private final String todo;
    private final long time = System.currentTimeMillis();

   public Todo(String todo) {
       this.todo = todo;
   }
}

You can run this curl command.

$  curl http://localhost:9090/mrs/todoservice/todoo
{"todo":"Hello","time":1436378304880}

With the ManagedServiceBuilder you can also get health and stats.

curl  http://localhost:9090/__stats/
{
    "MetricsKV": {},
    "MetricsMS": {},
    "MetricsC": {
        "TodoService": 1,
        "TodoService.startBatchCount": 2,
        "TodoService.receiveCount": 2
    },
    "version": 1
}

curl  http://localhost:9090/__stats_instance
{"MetricsKV":{},"MetricsMS":{},"MetricsC":{"TodoService.startBatchCount":1,"TodoService.receiveCount":1},"version":1}
$ curl  http://localhost:9090/__health
"ok"

There are flags to turn that off. And you also get a CTRL-C to nicely shutdown all of the services. :)

It also reads $WEB_PORT and then $PORT from the enviroment variables. This way is plays nice in Heroku or Docker or Mesos.

You can also get rid of the root URI altogher as follows:

    public static void main(String... args) {


        final ManagedServiceBuilder managedServiceBuilder = ManagedServiceBuilder.managedServiceBuilder();

        final ServiceEndpointServer serviceServer = managedServiceBuilder.getEndpointServerBuilder().setUri("/")
                .build()
                .initServices(new TodoService()).startServer();

        /* Wait for the service to shutdown. */
        managedServiceBuilder.getSystemManager().waitForShutdown();

    }

Then to curl you would do this:

$  curl http://localhost:9090/todoservice/todoo
{"todo":"Hello","time":143637868275}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment