# Multi-threaded WebServers tasks executor using HttpServer and WebSocket
This project aims to create two kinds of multi-threaded web servers (with thread-pooling) for executing simple tasks.
There are three tasks implemented and the user can add more (see details below).
The web servers receive the command and the parameter(s) for running a task through terminal command `curl`.
## WebServers
***WebServerHttp** is developed using *HttpServer*[javadoc](https://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpServer.html).
***WebServerSocket** is developed using *ServerSocket*[javadoc](https://docs.oracle.com/javase/8/docs/api/java/net/ServerSocket.html).
Both web servers use *ExecutorService*[javadoc](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html) that executes each submitted task in the pooled thread, more specifically, `Executors.newCachedThreadPool()` which is an unbounded thread pool with automatic thread reclamation [javadoc](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newCachedThreadPool--).
P.S. There is another project where I used *ServerSocker* in GitHub: [localhost-chat-socket](https://github.com/wagnerjfr/localhost-chat-socket)
## Tasks
The idea for the tasks comes from another GitHub project [TaskScheduler](https://github.com/wagnerjfr/Java-TaskScheduler).
The tasks currently implement are:
* TaskBubbleSort: sorts a list of numbers.
* TaskFiboRecursive: calculates the fibonacci of a number recursively
* TaskBitcoin: grabs the last price(s) of the bitcoin from [BitStamp](https://www.bitstamp.net/).
## How to add tasks
Steps to create a new task:
* Create a new class inside the package `task`, for example, `TaskHello.java`;
* Make sure your class extends the abstract class `TaskImpl.java`;
* Create a constructor and override the required method `execute()`;
* Add your logic and attibute a String response to the `response` instance variable.
Example:
```
package task;
import task.TaskImpl;
public class TaskHello extends TaskImpl {
public TaskHello(String input) {
super(input);
}
@Override
public void execute() {
result = "Hello " + input;
}
}
```
* Change `singleton/TaskExecutor.java` by adding a new "case" in the switch-case context for your task.
Example:
```
...
case "TaskHello":
task = new TaskHello(input);
break;
default:
...
```
* When the project is compiled, up and running, the task can be called like:
`curl -d TaskHello -d World http://localhost:8000/`
Something like this should be expected as output:
```console
wfranchi@computer:~$ curl -d TaskHello -d World http://localhost:8000
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5f3564b1e3bd taskwebserver:1.0 "/bin/sh -c 'java $W…" 3 seconds ago Up 2 seconds 0.0.0.0:8001->8000/tcp webserverhttp
83a07b12b36c taskwebserver:1.0 "/bin/sh -c 'java $W…" 18 seconds ago Up 17 seconds 0.0.0.0:8000->8000/tcp webserversocket
```
## Running the tasks
To check whether the web servers can handle more than one request in parallel, you can open different terminal (or console) and run the examples commands below.
Replace `http://localhost:8000/` to `http://localhost:8001/` to run the same task but in a different web server.