有人在 Heroku 上成功部署了 GWT 应用程序吗?

发布于 2024-12-03 18:33:10 字数 104 浏览 1 评论 0原文

Heroku 最近开始支持 Java 应用程序。浏览文档,它似乎类似于 Java Servlet 标准。有谁知道 GWT 应用程序已成功部署在 Heroku 上的实例吗?如果可以,有什么限制吗?

Heroku recently began supporting Java apps. Looking through the docs, it seems to resemble the Java Servlet Standard. Does anyone know of an instance where a GWT app has been successfully deployed on Heroku? If so, are there any limitations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风柔一江水 2024-12-10 18:33:10

是的,我已经使用此处的 Java 入门说明成功进行了部署:
http://devcenter.heroku.com/articles/java

我使用带有 appassembler 插件的 Maven 项目方法,但添加了 gwt-maven-plugin 以在构建过程中编译 GWT 应用程序。

当您推送到 heroku 时,您会看到 GWT 编译进程在一个线程上运行,速度非常慢,但运行良好。

嵌入式 Jetty 实例配置为在 src/main/resources/static 的 /static 处提供静态资源,我在构建期间将编译的 GWT 应用程序复制到此位置,然后正常引用 .nocache.js。

您还想了解什么?

您有一个选择,要么将 GWT 应用程序的 Javascript 表示形式本地构建到您的 Maven 项目中,提交它并从您的应用程序中读取它,要么像我提到的那样通过 gwt-maven-plugin 在 Heroku 中生成它。

通过嵌入式 Jetty 从 jar 内的静态位置提供文件的代码在 Guice ServletModule 中是这样的:(

请参阅下面我的其他答案,了解更简单且更少 Guice 驱动的方法来执行此操作。)

protected void configureServlets() {
  bind(DefaultServlet.class).in(Singleton.class);
  Map<String, String> initParams = new HashMap<String, String>();
  initParams.put("pathInfoOnly", "true");
  initParams.put("resourceBase", staticResourceBase());
  serve("/static/*").with(DefaultServlet.class, initParams);
}

private String staticResourceBase() {
    try {
        return WebServletModule.class.getResource("/static").toURI().toString();
    }
    catch (URISyntaxException e) {
        e.printStackTrace();
        return "couldn't resolve real path to static/";
    }
}

还有一些其他技巧要让嵌入式 Jetty 与 guice-servlet 一起工作,请告诉我这是否还不够。

Yes, I've got a successful deployment using the getting started with Java instructions here:
http://devcenter.heroku.com/articles/java

I use the Maven project with appassembler plugin approach but added gwt-maven-plugin to compile a GWT app during the build.

When you push to heroku you see the GWT compile process running, on one thread only so quite slow but it works fine.

The embedded Jetty instance is configured to serve up static resources at /static from src/main/resources/static and I copy the compiled GWT app to this location during the build and then reference the .nocache.js as normal.

What else do you want to know?

You've got a choice, either build the Javascript representation of your GWT app locally into your Maven project, commit it and the read it from your app, or to generate it inside Heroku via the gwt-maven-plugin as I mentioned.

The code to serve up files from a static location inside your jar via embedded Jetty is something like this inside a Guice ServletModule:

(See my other answer below for a simpler and less Guice-driven way to do this.)

protected void configureServlets() {
  bind(DefaultServlet.class).in(Singleton.class);
  Map<String, String> initParams = new HashMap<String, String>();
  initParams.put("pathInfoOnly", "true");
  initParams.put("resourceBase", staticResourceBase());
  serve("/static/*").with(DefaultServlet.class, initParams);
}

private String staticResourceBase() {
    try {
        return WebServletModule.class.getResource("/static").toURI().toString();
    }
    catch (URISyntaxException e) {
        e.printStackTrace();
        return "couldn't resolve real path to static/";
    }
}

There's a few other tricks to getting embedded Jetty working with guice-servlet, let me know if this isn't enough.

执手闯天涯 2024-12-10 18:33:10

当 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/ 下。

My first answer to this turned out to have problems when GWT tried to read its serialization policy. In the end I went for a simpler approach that was less Guice-based. I had to step through the Jetty code to understand why setBaseResource() was the way to go - it's not immediately obvious from the Javadoc.

Here's my server class - the one with the main() method that you point Heroku at via your app-assembler plugin as per the Heroku docs.

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 is a GuiceServletContextListener.

You then put your static resources under src/main/resources/static/.

遮了一弯 2024-12-10 18:33:10

理论上,人们应该能够使用 Jetty 或 Tomcat 的嵌入式版本运行 GWT,并在 main 中引导服务器,如 Heroku Java 文档中所述。

In theory, one should be able to run GWT using the embedded versions of Jetty or Tomcat, and bootstrap the server in main as described in the Heroku Java docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文