User Tools

Site Tools


java_jetty_server_on_metaarray

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
java_jetty_server_on_metaarray [2022/01/20 19:40] – created peteyboyjava_jetty_server_on_metaarray [2023/04/27 18:26] (current) – [Setting up a simple Java Jetty Server] fix typo, and sentence peteyboy
Line 1: Line 1:
-FIXME  this is under construction 
- 
  
 ====== Setting up a simple Java Jetty Server  ====== ====== Setting up a simple Java Jetty Server  ======
Line 9: Line 7:
 You can create a java server project that embeds Jetty and run it on [[metaarray|the MetaArray]] on your [[meta_port_allocation|assigned port]]. Below is a very simple example on how to get started with that. It doesn't use any IDE, so it's a bit manual. You can find more detailed directions at the external project links below. You can create a java server project that embeds Jetty and run it on [[metaarray|the MetaArray]] on your [[meta_port_allocation|assigned port]]. Below is a very simple example on how to get started with that. It doesn't use any IDE, so it's a bit manual. You can find more detailed directions at the external project links below.
  
-There are lots of different java frameworks that center around using the Jetty server, including a few micro frameworksI am going to show an example using [[https://javalin.io | Javalin]], which is a microframework that is a lighter load that Spring Boot and other more mainline frameworks.  It has a simple tutorial that was used and modified a bit here to show you how to set up a Java Jetty server. To make things quick to set up, we'll use Maven (''mvn'').+There are lots of different java web frameworks that center around using the [[https://www.eclipse.org/jetty/ |Jetty]] server, including a few microframeworksThis page is going to go through an example using [[https://javalin.io | Javalin]], which is a microframework that has a lighter footprint and is easier to configure than Spring Boot and other more mainline frameworks.  It has a simple tutorial that was used and modified a bit here to show you how to set up a Java Jetty server. To make things quick to set up, we'll use Maven (''mvn'').
  
 ===== Create Project Space ===== ===== Create Project Space =====
  
-First, we'll create your maven-style project folder.+First, let'create your maven-style project folder.
   - Go to an appropriate project folder that you want to work in   - Go to an appropriate project folder that you want to work in
   - make the java source folder in the maven style   - make the java source folder in the maven style
Line 21: Line 19:
 ===== Create Project Object Model ===== ===== Create Project Object Model =====
  
-Next, we'll set up the POM file, which maven will use to pull all the dependencies for our framework and let us build it.+Next, let'set up the POM file, which maven will use to pull all the dependencies for our framework and let us build it. 
 + 
 +  - Create a new file in "myproject", call it ''pom.xml'' 
 +    * ''nano pom.xml'' 
 +  - Paste in the following: <code> 
 +<project> 
 +    <!-- model version - always 4.0.0 for Maven 2.x POMs --> 
 +   <modelVersion>4.0.0</modelVersion> 
 + 
 +  <!-- project coordinates - values which uniquely identify this project --> 
 + <groupId>myproject.helloWorld</groupId> 
 +<artifactId>helloWorld</artifactId> 
 +    <version>1.0.0-SNAPSHOT</version> 
 +<properties> 
 +        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
 +        <maven.compiler.target>1.8</maven.compiler.target> 
 +</properties> 
 +   <!-- library dependencies --> 
 +  <dependencies> 
 +<dependency> 
 +  <groupId>io.javalin</groupId> 
 +    <artifactId>javalin</artifactId> 
 + <version>4.3.0</version> 
 +    </dependency> 
 +    <dependency> 
 +      <groupId>org.slf4j</groupId> 
 +      <artifactId>slf4j-simple</artifactId> 
 +      <version>1.7.31</version> 
 +     </dependency> 
 +   </dependencies> 
 +     <build> 
 +    <plugins> 
 +      <plugin> 
 +        <artifactId>maven-compiler-plugin</artifactId> 
 +        <version>3.6.0</version> 
 +        <configuration> 
 +          <source>1.8</source> 
 +          <target>1.8</target> 
 +          <showDeprecation/> 
 +          <showWarnings/> 
 +          <compilerArgs> 
 +            <arg>-Xlint:unchecked</arg> 
 +            <arg>-Xlint:all</arg> 
 +            <arg>-Xlint:-serial</arg> 
 +          </compilerArgs> 
 +        </configuration> 
 +      </plugin> 
 +      <plugin> 
 +        <groupId>org.apache.maven.plugins</groupId> 
 +        <artifactId>maven-surefire-plugin</artifactId> 
 +        <version>2.19.1</version> 
 +        <configuration> 
 +          <systemPropertyVariables> 
 +            <java.util.logging.config.file>src/main/resources/logging.properties</java.util.logging.config.file> 
 +          </systemPropertyVariables> 
 +        </configuration> 
 +      </plugin> 
 +      <plugin> 
 +        <!-- Exec plugin lets us run app directly from maven and not deal with classpaths --> 
 +       <groupId>org.codehaus.mojo</groupId> 
 +         <artifactId>exec-maven-plugin</artifactId> 
 +         <version>3.0.0</version> 
 +         <executions> 
 +           <execution> 
 +             <goals> 
 +               <goal>java</goal> 
 +             </goals> 
 +           </execution> 
 +         </executions> 
 +         <configuration> 
 +           <!-- This is what exec will run as project main, *CHANGE THIS IF YOU ARE USING A DIFFERENT MAIN CLASS--> 
 +           <mainClass>myproject.helloWorld</mainClass> 
 +         </configuration> 
 +       </plugin> 
 +      </plugins> 
 +    </build> 
 +</project> 
 + 
 +</code> 
 +  - Save the file 
 + 
 +===== Add Your Java Code ===== 
 + 
 + - Using an editor again, create your hello world class file, in its proper folder for maven: 
 +    - ''nano src/main/java/myproject/helloWorld.java'' 
 +    - paste in the following: <code java> 
 +package myproject; 
 + 
 +import io.javalin.Javalin; 
 + 
 +public class helloWorld { 
 +    private static int MY_JETTY_PORT= 8388;  //replace 8388 with your port as explained in the wiki 
 +     
 +    public static void main(String[] args){ 
 +        Javalin app = Javalin.create().start(MY_JETTY_PORT); 
 +        app.get("/", ctx -> ctx.result("Hello World, this is my Java App on Jetty!")); 
 +    } 
 +
 +</code> 
 +  - move the cursor up and change the value of MY_JETTY_PORT to be your [[meta_port_allocation|Port on the MetaArray]] (that is, replace 8388 with your UID number). 
 +  - save the file 
 + 
 +===== Build Your Project =====
  
 +Here we go. Back at the command line, type
 +  mvn package
 + 
 +You will see a bunch of log info scroll by, but at the end, you should see something like this:
  
 +  [INFO] ------------------------------------------------------------------------
 +  [INFO] BUILD SUCCESS
 +  [INFO] ------------------------------------------------------------------------
 +  [INFO] Total time:  3.537 s
 +  [INFO] Finished at: 2022-01-20T21:04:04Z
 +  [INFO] ------------------------------------------------------------------------
 +  
 +  
 +===== Run Your Server =====
  
 +Now, let's run the server. We've set up an "exec goal" in the POM so we can run the resulting project from maven, like so:
  
 +  mvn exec:java
 +  
 +You should see something like this (hopefully any warnings you get are ignorable):
  
 +  WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
 +  WARNING: All illegal access operations will be denied in a future release
 +  [INFO] Scanning for projects...
 +  [INFO] 
 +  [INFO] ------------------< myproject.helloWorld:helloWorld >-------------------
 +  [INFO] Building helloWorld 1.0.0-SNAPSHOT
 +  [INFO] --------------------------------[ jar ]---------------------------------
 +  [INFO] 
 +  [INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ helloWorld ---
 +  [myproject.helloWorld.main()] INFO io.javalin.Javalin -  
 +         __                      __ _            __ __
 +        / /____ _ _   __ ____ _ / /(_)____      / // /
 +   __  / // __ `/| | / // __ `// // // __ \    / // /_
 +  / /_/ // /_/ / | |/ // /_/ // // // / / /   /__  __/
 +  \____/ \__,_/  |___/ \__,_//_//_//_/ /_/      /_/  
 +  
 +          https://javalin.io/documentation
 +  
 +  [myproject.helloWorld.main()] INFO org.eclipse.jetty.util.log - Logging initialized @2496ms to org.eclipse.jetty.util.log.Slf4jLog
 +  [myproject.helloWorld.main()] INFO io.javalin.Javalin - Starting Javalin ...
 +  [myproject.helloWorld.main()] INFO io.javalin.Javalin - You are running Javalin 4.3.0 (released January 13, 2022).
 +  [myproject.helloWorld.main()] INFO io.javalin.Javalin - Listening on http://localhost:32056/
 +  [myproject.helloWorld.main()] INFO io.javalin.Javalin - Javalin started in 263ms \o/
 +  
 +If you get here, then your server is running! Now try it out: point your web browser to //http://yourusername.yourSDFmetarraydomain.org:yourMetaArrayPort // and see it if works!
  
 +Press ''ctl-c'' to stop the server. 
  
 +The rest is up to your imagination!
  
java_jetty_server_on_metaarray.1642707628.txt.gz · Last modified: 2022/01/20 19:40 by peteyboy