|
| 1 | += Spring Verticle Factory Example |
| 2 | + |
| 3 | +This project shows how you can build a Verticle factory based on the Spring container. |
| 4 | + |
| 5 | +Why not injecting a verticle instance and deploy it directly? |
| 6 | +Because Vert.x follows the Multi-Reactor pattern, |
| 7 | +so if you want to scale accross your server's CPU, |
| 8 | +you need to http://vertx.io/docs/vertx-core/java/#_specifying_number_of_verticle_instances[deploy multiple instances of your verticle]. |
| 9 | + |
| 10 | +Only then Vert.x will guarantee that the instances run on separate event loops. |
| 11 | + |
| 12 | +== The code |
| 13 | + |
| 14 | +The `io.vertx.examples.spring.verticlefactory.SpringVerticleFactory` class is an `ApplicationContextAware` Spring component. |
| 15 | +It is given an `ApplicationContext` on startup and will use it to create verticle instances. |
| 16 | +As it is managed by the Spring container, it must be registered manually (usually one can do this via the Java service loaders). |
| 17 | + |
| 18 | +The `io.vertx.examples.spring.verticlefactory.SpringVerticleFactory` depends on a simple greeter component. |
| 19 | +It sets up an HttpServer and use the greeter to generate a hello message for the HTTP response. |
| 20 | + |
| 21 | +IMPORTANT: When you invoke Spring beans from standard verticles, remember that you must not call blocking code |
| 22 | +or you will block the event loop. |
| 23 | + |
| 24 | +== Trying |
| 25 | + |
| 26 | + |
| 27 | +In a terminal, compile and run the example: |
| 28 | + |
| 29 | +[source,shell] |
| 30 | +---- |
| 31 | +mvn compile exec:java@run |
| 32 | +---- |
| 33 | + |
| 34 | +Then in another tab or window, send a few requests to the server. |
| 35 | + |
| 36 | +[source,shell] |
| 37 | +---- |
| 38 | +seq 10 | while read i; do curl http://localhost:8080/?name=Thomas${i}; echo; done |
| 39 | +---- |
| 40 | + |
| 41 | +If you look at the console, you'll notice that the requests have indeed been managed by different event loops: |
| 42 | + |
| 43 | +[source] |
| 44 | +[subs="verbatim,quotes"] |
| 45 | +---- |
| 46 | +12:46:02.661 [*vert.x-eventloop-thread-2*] INFO io.vertx.examples.spring.verticlefactory.GreetingVerticle - Got request for name: Thomas1 |
| 47 | +12:46:02.685 [*vert.x-eventloop-thread-4*] INFO io.vertx.examples.spring.verticlefactory.GreetingVerticle - Got request for name: Thomas2 |
| 48 | +12:46:02.698 [*vert.x-eventloop-thread-1*] INFO io.vertx.examples.spring.verticlefactory.GreetingVerticle - Got request for name: Thomas3 |
| 49 | +12:46:02.711 [*vert.x-eventloop-thread-3*] INFO io.vertx.examples.spring.verticlefactory.GreetingVerticle - Got request for name: Thomas4 |
| 50 | +12:46:02.722 [*vert.x-eventloop-thread-2*] INFO io.vertx.examples.spring.verticlefactory.GreetingVerticle - Got request for name: Thomas5 |
| 51 | +12:46:02.733 [*vert.x-eventloop-thread-4*] INFO io.vertx.examples.spring.verticlefactory.GreetingVerticle - Got request for name: Thomas6 |
| 52 | +12:46:02.748 [*vert.x-eventloop-thread-1*] INFO io.vertx.examples.spring.verticlefactory.GreetingVerticle - Got request for name: Thomas7 |
| 53 | +12:46:02.761 [*vert.x-eventloop-thread-3*] INFO io.vertx.examples.spring.verticlefactory.GreetingVerticle - Got request for name: Thomas8 |
| 54 | +12:46:02.773 [*vert.x-eventloop-thread-2*] INFO io.vertx.examples.spring.verticlefactory.GreetingVerticle - Got request for name: Thomas9 |
| 55 | +12:46:02.785 [*vert.x-eventloop-thread-4*] INFO io.vertx.examples.spring.verticlefactory.GreetingVerticle - Got request for name: Thomas10 |
| 56 | +---- |
0 commit comments