有人在Heroku上成功部署了GWT应用程序吗?
Heroku最近开始支持Java应用程序。查看文档,它似乎类似于JavaServlet标准。有人知道在Heroku上成功部署了GWT应用程序的实例吗?如果是这样的话,有什么限制吗?理论上,应该能够使用Jetty或Tomcat的嵌入式版本运行GWT,并按照Heroku Java文档中的描述在有人在Heroku上成功部署了GWT应用程序吗?,gwt,heroku,Gwt,Heroku,Heroku最近开始支持Java应用程序。查看文档,它似乎类似于JavaServlet标准。有人知道在Heroku上成功部署了GWT应用程序的实例吗?如果是这样的话,有什么限制吗?理论上,应该能够使用Jetty或Tomcat的嵌入式版本运行GWT,并按照Heroku Java文档中的描述在main中引导服务器。是的,我已经使用下面的Java入门说明成功部署了: 我使用带有appassembler插件的Maven项目,但在构建过程中添加了gwt Maven插件来编译gwt应用程序 当您推到her
main
中引导服务器。是的,我已经使用下面的Java入门说明成功部署了:
我使用带有appassembler插件的Maven项目,但在构建过程中添加了gwt Maven插件来编译gwt应用程序
当您推到heroku时,您会看到GWT编译进程在一个线程上运行,虽然速度非常慢,但工作正常
嵌入式Jetty实例被配置为在src/main/resources/static的/static处提供静态资源,我在构建过程中将编译好的GWT应用程序复制到此位置,然后正常引用.nocache.js
你还想知道什么
您有一个选择,要么在本地将GWT应用程序的Javascript表示构建到Maven项目中,提交并从应用程序中读取它,要么通过GWT Maven插件在Heroku内部生成它,正如我提到的
通过嵌入式Jetty从jar中的静态位置提供文件的代码类似于Guice ServletModule中的以下代码:
(请参阅下面我的另一个答案,了解一种更简单、更不受Guice驱动的方法。)
protectedvoid-configureServlets(){
bind(DefaultServlet.class).in(Singleton.class);
Map initParams=new HashMap();
initParams.put(“pathInfoOnly”、“true”);
put(“resourceBase”,staticResourceBase());
使用(DefaultServlet.class,initParams)服务(“/static/*”);
}
私有字符串staticResourceBase(){
试一试{
返回WebServletModule.class.getResource(“/static”).toURI().toString();
}
捕获(URISyntaxException e){
e、 printStackTrace();
return“无法解析到static/的实际路径”;
}
}
让嵌入式Jetty使用guice servlet还有其他一些技巧,如果这还不够,请告诉我。当GWT试图读取其序列化策略时,我的第一个答案出现了问题。最后,我选择了一种更简单的方法,这种方法不太基于Guice。我必须一步一步地阅读Jetty代码,才能理解为什么
setBaseResource()
是一条可行之路——这在Javadoc中并不明显
这是我的服务器类——一个带有main()方法的类,根据Heroku文档,您可以通过应用程序汇编插件将Heroku指向它
public class MyServer {
public static void main(String[] args) throws Exception {
if (args.length > 0) {
new MyServer().start(Integer.valueOf(args[0]));
}
else {
new MyServer().start(Integer.valueOf(System.getenv("PORT")));
}
}
public void start(int port) throws Exception {
Server server = new Server(port);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setBaseResource(createResourceForStatics());
context.setContextPath("/");
context.addEventListener(new AppConfig());
context.addFilter(GuiceFilter.class, "/*", null);
context.addServlet(DefaultServlet.class, "/");
server.setHandler(context);
server.start();
server.join();
}
private Resource createResourceForStatics() throws MalformedURLException, IOException {
String staticDir = getClass().getClassLoader().getResource("static/").toExternalForm();
Resource staticResource = Resource.newResource(staticDir);
return staticResource;
}
}
AppConfig.java是一个GuiceServletContextListener
然后,您将静态资源放在
src/main/resources/static/
下,我发现了一种更简单的方式来提供静态文件,例如嵌入式Jetty中的GWT应用程序表单。如果你想让我发布,请在这里发表评论。嘿,你能发布你的更简单的解决方案吗?@MattR你能发布你的更简单的方法吗?如果有博客的话,你能给我们指一指吗?它已经在这里了-但它是上面的,而不是下面最初所说的。见公认的答案。
public class MyServer {
public static void main(String[] args) throws Exception {
if (args.length > 0) {
new MyServer().start(Integer.valueOf(args[0]));
}
else {
new MyServer().start(Integer.valueOf(System.getenv("PORT")));
}
}
public void start(int port) throws Exception {
Server server = new Server(port);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setBaseResource(createResourceForStatics());
context.setContextPath("/");
context.addEventListener(new AppConfig());
context.addFilter(GuiceFilter.class, "/*", null);
context.addServlet(DefaultServlet.class, "/");
server.setHandler(context);
server.start();
server.join();
}
private Resource createResourceForStatics() throws MalformedURLException, IOException {
String staticDir = getClass().getClassLoader().getResource("static/").toExternalForm();
Resource staticResource = Resource.newResource(staticDir);
return staticResource;
}
}