question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Multi Threaded Routing Module Servelet

See original GitHub issue

How do you create a multi-threaded routing module servelet so all available CPU cores are utilised?

Also in the routing module servelet how do you make a distinction on what type of request is made when the method is not provided? Within this context how can parameters be accessed?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eduard-vasinskyicommented, Jul 8, 2020

Hello, @sirinath RouterModule is supported only for single-threaded servers. You have to map servlets to corresponding paths by hand, similarly to example from this reply

// Also at this point if I want to read a cookie how do I do it?

You can read a cookie directly from a request by calling either @Nullable String getCookie(String) - to read a single cookie or Map<String,String> getCookies() - to retrieve all of the cookies from a request

You can refer to API of HttpRequest if you need help with accessing cookies or other request parameters. Or you can look at examples on the website that I have provided in previous reply.

Also, what does this piece of code do in the example on your site

Modules.combine() allows you to combine bindings from different modules as if it is a single module. If your application has several modules, you could use Modules.combine() to return a single module from getBusinessLogicModule() method or getOverrideModule().

Also how does getBusinessLogicModule() compare with getOverrideModule()?

If you specify a duplicate binding in getBusinessLogicModule() you will catch an exception, as bindings with same keys are forbidden. However, if you want to override existing bindings with your own, you can use getOverrideModule() for this. For example, you can override default Config provided in HttpServerLauncher by overriding getOverrideModule() and returning a module that contains binding for a new Config.

You can refer to documentation for more information. Or you can look at API/javadocs of Injector, Launcher or HttpServerLauncher

1reaction
eduard-vasinskyicommented, Jul 7, 2020

Hello, @sirinath Yes, you can use routing servlet in multithreaded context. The preffered way is to specify all of the mappings directly. If you wish, you can omit HTTP method while configuring routes in RoutingServlet. You can determine a method of HTTP request based on result of request.getMethod() call. But it is more convenient to specify the method to be handled. Mappings that do not specify HTTP method are primarly useful for providing fallback servlets, or for combining with other routing servlets that have mappings for multiple methods.

Here is a snippet that demonstrates how to do that:

package io.activej.launchers.http;

import io.activej.http.AsyncServlet;
import io.activej.http.HttpResponse;
import io.activej.http.RoutingServlet;
import io.activej.inject.Injector;
import io.activej.inject.annotation.Named;
import io.activej.inject.annotation.Provides;
import io.activej.worker.annotation.Worker;
import io.activej.worker.annotation.WorkerId;

import static io.activej.http.HttpMethod.GET;
import static io.activej.http.HttpMethod.POST;

public final class ApplicationLauncher extends MultithreadedHttpServerLauncher {

	@Provides
	@Worker
	AsyncServlet servlet(@WorkerId int workerId, @Named("API") AsyncServlet apiServlet) {
		return RoutingServlet.create()
				.map(GET, "/", request -> HttpResponse.ok200()
						.withPlainText("hello world: " + workerId))
				.map("/allMethods", request -> HttpResponse.ok200()
						.withPlainText("Called with HTTP method: " + request.getMethod()))
				.map("/api/*", apiServlet);
	}

	@Provides
	@Worker
	@Named("API")
	AsyncServlet apiServlet(@WorkerId int workerId) {
		return RoutingServlet.create()
				.map(GET, "/resource", request -> HttpResponse.ok200()
						.withPlainText("Getting a resource from worker: " + workerId))
				.map(POST, "/resource", request -> HttpResponse.ok200()
						.withPlainText("Posting a resource to worker: " + workerId));
	}

	public static void main(String[] args) throws Exception {
		Injector.useSpecializer();

		new ApplicationLauncher().launch(args);
	}
}

Note how HTTP method of the request is accessed in /allMethods mapping. An API servlet that has mappings for different methods is mapped to /api/* endpoint that does not specify HTTP method. As a result, requests are correctly passed to the API servlet to be handled.

To learn more about servlets and how parameters are handled, please refer to examples: Multithreaded server Request parameters Routing servlet

Read more comments on GitHub >

github_iconTop Results From Across the Web

15+ Java multithreading interview questions & answers
Java multithreading interview questions & answers explain beginner level concepts like heap vs stack, re-entrant, synchronized keyword, ...
Read more >
Servlet and JSP Tutorial | How to Build Web Applications in ...
Introduction to Servlets. Servlet is a server-side Java program module that handles client requests and implements the servlet interface.
Read more >
how to use Servlet in multithread environment - Stack Overflow
All you have to care about is to makeyour servlet code thread-safe. And the best way to do it is to let it...
Read more >
Chapter 2 Using Servlets
The automatic routing in an HTTP servlet is based simply on a call to request.getMethod(), which provides the HTTP transfer method. In a...
Read more >
What are some of the toughest Java multi threading questions?
1. Implementing Runnable over extending Thread(from composition over inheritance perspective) 2. Synchronized collections vs concurrent collections(how they ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found