Question Bank 1 – Java and DropWizard

Hey! Every now and then, I keep coming up with various questions/blockers during the development phase. But because their answers are just too small (even one liner), I tend to avoid writing a post about it. Therefore, I am writing these “Question Banks” for myself to refer those questions/answers later if required.

Questions

  1. How to make an external HTTP request from Java/DropWizard?
  2. How to cache in Java/DropWizard?
  3. How to async in Java/DropWizard?
  4. How to debug in Java?

How to make an external HTTP request from Java/ DropWizard?

Like we can use “requests” module in Python, etc. What can we do in Dropwizard?

Resources:

import javax.ws.rs.client.ClientBuilder;

...
    // in a class, in a method
    Client client = ClientBuilder.newClient();
    String result = client.target("http://path_to_external_resource").request().get(String.class);

    return result;
...

How to cache in Java/DropWizard?

Resources:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.2.1</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>
package com.eduonix.redis.example
import redis.clients.jedis.Jedis; 

public class RedisCacheExample { 
   public static void main(String[] args) { 
      //Connect to Redis server using localhost
      Jedis jedis = new Jedis("localhost"); 
      System.out.println("Connection successful"); 	  
      //set string data in redis cache
      jedis.set("eduonix", "Redis cache tutorial"); 
      //Retrieve the string value from the cache 
      System.out.println("Stored data in cache: "+ jedis.get("eduonix")); 
   } 
}

How to async with Java/Dropwizard?

Resources:

@Path("/resource")
public class HelloResource {
    @GET
    public void asyncHelloWorld(@Suspended final AsyncResponse asyncResponse) {
        new Thread(() -> {
            sleepUninterruptibly(1000, MILLISECONDS);
            asyncResponse.resume("Hello world!");
        }).start();
    }
}

How to debug in Java?

Resources:

That’s all for now. Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s