[英]java how to run spark app within apache server
I have a Spark MVC app, which is very simple. 我有一个Spark MVC应用程序,非常简单。
According to spark documentation , this should be enough to run the app: 根据spark文档 ,这应该足以运行应用程序:
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. 我将该类放在项目中的一个包中,然后在Apache Tomcat服务器上运行Web应用程序(动态Web应用程序)。
The print statement doesn't appear when run Apache Tomcat, that means this class is not being called. 运行Apache Tomcat时不会出现print语句,这意味着此类未被调用。 I know it makes sense . 我知道这很有道理 。 that is why i am asking. 这就是我要问的原因。 please how can I let Apache Tomcat run my spark app? 请问如何让Apache Tomcat运行我的spark应用程序?
After @mlk answer , i did the following: @mlk回答后 ,我做了以下事情:
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: 在我的web.xml中我做了:
<?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
我的SparkServer在一个包中: com.recommender
存在于源文件夹中: src/main/java
I run apache tomcat, but still when i call any path from spark, it is returning 404. 我运行apache tomcat,但是当我从spark调用任何路径时,它仍然返回404。
HITE i can run spark from the main method and call my pages, they are working. HITE我可以从main方法运行spark并调用我的页面,它们正在工作。 so the problem with the way i configured spark to run in apache tomcat 所以我配置spark的方式问题在apache tomcat中运行
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);
});
Spark comes with a container built in, so you don't need tomcat or jetty. Spark附带一个内置容器,因此您不需要tomcat或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
. 如果要部署到完整的容器,则可以创建web.xml文件并实现spark.servlet.SparkApplication
。
Edit: you are missing the applicationClass property in your Web.xml. 编辑:您缺少Web.xml中的applicationClass属性。
WebApps don't execute static void main()
methods, they bootstrap according to their web.xml
from their deployed WAR file. WebApps不执行static void main()
方法,它们根据其部署的WAR文件中的web.xml
引导。
Do you have any experience with running webapps? 你有运行webapps的经验吗? You need a container like Tomcat or Jetty. 你需要一个像Tomcat或Jetty这样的容器。 Apache server is just for serving HTTP and is not an application container in itself. Apache服务器仅用于提供HTTP,并且本身不是应用程序容器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.