Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java how to run spark app within apache server

I have a Spark MVC app, which is very simple.

According to spark documentation, this should be enough to run the app:

public class SparkServer {
    public static void main(String args[]) {
        Spark.staticFileLocation("src/main/webapp");
        System.out
                .println("bla bla bla");
        RService rService = new SparqlRService();
        new RController(rService);
    }
}

I put that class in a package inside my project, and I run the web app (dynamic web app) on Apache Tomcat server.

The print statement doesn't appear when run Apache Tomcat, that means this class is not being called. I know it makes sense. that is why i am asking. please how can I let Apache Tomcat run my spark app?

Update

After @mlk answer, i did the following:

public class SparkServer implements SparkApplication {

    @Override
    public void init() {
        Spark.staticFileLocation("src/main/webapp");
        System.out
                .println("bla bla lba");
        RService rService = new SparqlRService();
        new RController(rService);
    }
}

and in my web.xml i did:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>SRecommender</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <filter>
        <filter-name>SparkFilter</filter-name>
        <filter-class>spark.servlet.SparkFilter</filter-class>
        <init-param>
            <param-name>
    applicationClass</param-name>
            <param-value>com.srecommender.SparkServer</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>SparkFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

Where my SparkServer is in a package: com.recommender that exists in a source folder: src/main/java

I run apache tomcat, but still when i call any path from spark, it is returning 404.

HITE i can run spark from the main method and call my pages, they are working. so the problem with the way i configured spark to run in apache tomcat

Update 2

This is how to set the path of the view

public RecommendationController(RecommendationService service) {
        get("/", (request, response) -> {

            Map<String, Object> model = new HashMap<>();
            model.put("data", "forza ROMA");
            model.put("data2", "It's Rome, It's home");

            return View("src/main/webapp/template/index.html", model);
        });
like image 374
Marco Dinatsoli Avatar asked Oct 18 '22 14:10

Marco Dinatsoli


2 Answers

Spark comes with a container built in, so you don't need tomcat or jetty. If you want to deploy to a full blow container then you can by creating a web.xml file and implementing spark.servlet.SparkApplication.

Edit: you are missing the applicationClass property in your Web.xml.

like image 116
Michael Lloyd Lee mlk Avatar answered Oct 21 '22 03:10

Michael Lloyd Lee mlk


WebApps don't execute static void main() methods, they bootstrap according to their web.xml from their deployed WAR file.

Do you have any experience with running webapps? You need a container like Tomcat or Jetty. Apache server is just for serving HTTP and is not an application container in itself.

like image 24
Brad Avatar answered Oct 21 '22 03:10

Brad