Implementing DELETE operation

So far, we've created, read, and updated a blog post. Now, let's implement and make a call to finally delete a blog post.

What you need

You need to have the files obtained from previous steps:

1. Implement server-side

Let's implement a service method deleteBlogPost() in we've been working on.

  1. Add a service method, deleteBlogPost().
    public class BlogService extends BlogServiceGrpc.BlogServiceImplBase {
      public void deleteBlogPost(DeleteBlogPostRequest request, StreamObserver<Empty> responseObserver) {
        try {
          // Simulate a blocking API call.
        } catch (InterruptedException e) {
          throw new RuntimeException(e);
        final BlogPost removed = blogPosts.remove(request.getId());
        if (removed == null) {
            new BlogNotFoundException("The blog post does not exist. ID: " + request.getId()));
        } else {
  2. Add Armeria's GrpcServiceBuilder's blocking task executor to make all gRPC methods executed in the blocking task executor's thread pool.

    By default, service methods are executed on the event loop and are expected to be implemented asynchronously. To implement blocking logic, call useBlockingTaskExecutor(true).
    private static Server newServer(int port) throws Exception {
      final GrpcService grpcService =
              GrpcService.builder().addService(new BlogService())
                                   .exceptionMapping(new GrpcExceptionHandler())

2. Implement client-side

Add a method deleteBlogPost() to send a request to update a blog post.

class BlogClient {
  static void deleteBlogPost(int id) {
    final DeleteBlogPostRequest request = DeleteBlogPostRequest.newBuilder().setId(id).build();
    try {
    } catch (StatusRuntimeException statusException) {
      // Handle exception

3. Test run

Let's test deleting a blog. We know that our client creates a blog post by default when it's executed and that the ID of the first one has been 0 always.

  1. Call the delete method we just implemented followed by listBlogPosts() to see that no post with the ID 0 exists after deleting the post.
    static void testRun() {
      createBlogPost("Another blog post", "Creating a post via createBlogPost().");
  2. Re-run the server. Your server is running if you see the following message.

    [armeria-boss-http-*:8080] INFO com.linecorp.armeria.server.Server - Serving HTTP at /[0:0:0:0:0:0:0:0]:8080 -
  3. Run the client. The number of blog posts in the log depends on how you've tested so far. Anyhow, since we are attempting to delete the first post, you won't have a problem, unless you've deleted it already. You can check that the list of posts retrieved start from 1, which is the second post.

    [armeria-common-worker-nio-2-1] DEBUG c.l.a.client.logging.LoggingClient - [creqId=8573ef34, ...
    Response: ..., content=CompletableRpcResponse{blogs {
    id: 1
  4. Now, while keeping the server running, run the client again, attempting to delete the post with the ID 0, which does not exist any longer.

    You should see the exception logged as the following.

    io.grpc.StatusException: NOT_FOUND: The blog post does not exist. ID: 0

What's next

Here, we've implemented a service method and client method for deleting a blog post. Next, we'll finally add Armeria's Documentation service to our service.