作成中のアプリケーションのDropwizard のversion を
1.0.6 > 1.1.2 に upgrade したところ、
コンパイルエラーが出力されました。

コンパイルエラー情報を元に、検索したところ、
以下、Jetty のupgrade 情報が該当しました。
Chapter 36. Upgrading Jetty

上記を参考にエラー発生箇所の対処を行ったので、実施した結果を記載します。


変更点について

修正前の実装

問題が出たのは、SessionHandler の設定箇所です。
ともと、Dropwizard で MongoDB session clustering を使う | Monotalk
実装を作成した部分となります。

    /**
     * setSessionHandlerTo
     *
     * @param env
     */
    private void setSessionHandlerTo(Environment env) throws UnknownHostException {
        // Set SessionHandler
        MongoClientURI mongoURI = new MongoClientURI(MongoDBResource.getUriString());
        // Not Close...
        MongoClient mongoClient = new MongoClient(mongoURI);
        // 非推奨だが、ライブラリが対応していないので使う..
        DB db = mongoClient.getDB("jetty_session");
        DBCollection collection = db.getCollection("jetty_session_collection");
        env.lifecycle().addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() {
            @Override
            public void lifeCycleStarting(LifeCycle event) {
                log.info("lifeCycleStarting start event = {}", event);
                if (!(event instanceof Server)) {
                    return;
                }
                Server server = (Server) event;
                MongoSessionIdManager sessionIdManager = new MongoSessionIdManager(server, collection);
                MongoSessionManager sessionManager = null;
                try {
                    sessionManager = new MongoSessionManager();
                } catch (UnknownHostException e) {
                    throw new IllegalStateException(e);
                }
                sessionIdManager.setWorkerName("node1");
                sessionIdManager.setScavengePeriod(1800000L);
                sessionIdManager.setScavengeBlockSize(0);
                sessionIdManager.setPurge(true);
                sessionIdManager.setPurgeDelay(1800000L);
                sessionIdManager.setPurgeInvalidAge(1800000L);
                sessionIdManager.setPurgeValidAge(5400000L);
                sessionIdManager.setPurgeLimit(0);

                sessionManager.setSessionIdManager(sessionIdManager);
                sessionManager.setPreserveOnStop(true);
                sessionManager.setHttpOnly(true);
                sessionManager.setMaxInactiveInterval(1800);
                env.servlets().setSessionHandler(new SessionHandler(sessionManager));
            }
        });
    }

変更点1. SessionManager が削除された

SessionManager の が削除されて、別のクラスに機能が分割された
SessionManager削除された影響で、 以下のようなエラーが出力されていました。

  期待値: 引数がありません
  検出値: org.eclipse.jetty.nosql.mongodb.MongoSessionManager
  理由: 実引数リストと仮引数リストの長さが異なります

SessionManager削除されて、機能がSessionHandler移動されているようなので、
実装を修正しました。

変更点2. Session をストレージに 登録する Module が変更された

作成しているアプリケーションは、MongoDB を Session ストレージとして使用していますが、
Jetty 9.3 では、nosql だったのが、
Jetty 9.4 では、session-store-mongo変更されています。

Clustered Session Management: MongoDB

jetty コマンドを オプション指定で起動すると、jarが落ちてくるようですが、
dropwizard 経由なのでやり方がよくわかりません。

再度、 Chapter 36. Upgrading Jetty
確認したところ、以下の記載を見つけました。

SessionIdManager Previously there was a different class of SessionIdManager - with different configuration options - depending upon which type of >clustering technology chosen. In Jetty 9.4, there is only one type, the org.eclipse.jetty.server.session.DefaultSessionIdManager.

DefaultSessionIdManager使うと変更ができそうだったので、そちらを使用するように変更しました。

変更点3. 修正はし、コンパイルは通ったが、起動しない。

コンパイルは通るようになりましたが、 以下メッセージが出力され、 server コマンドで、起動しても途中で止まるようになりました。

org.eclipse.jetty.nosql.mongodb.MongoSessionDataStoreFactory.getSavePeriodSec()I

どうも、jetty version と、session-store-mongo噛み合っておらず、
session-store-mongoほうが新しかったため発生していました。

修正後の実装

修正後の実装は以下のようになりました。
いくつか、MongoSessionIdManager から削除されたが、DefaultSessionIdManager には存在しないオプションがあり、
どうやって指定するのかわからないものがありますが、動作はするようになりました。

    /**
     * setSessionHandlerTo
     *
     * @param env
     */
    private void setSessionHandlerTo(Environment env) throws UnknownHostException {
        env.lifecycle().addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() {
            @Override
            public void lifeCycleStarting(LifeCycle event) {
                log.info("lifeCycleStarting start event = {}", event);
                if (!(event instanceof Server)) {
                    return;
                }
                Server server = (Server) event;
                MongoSessionDataStoreFactory storeFactory = new MongoSessionDataStoreFactory();
                storeFactory.setHost(MongoDBResource.getHost());
                storeFactory.setPort(Integer.valueOf(MongoDBResource.getPort()));
                storeFactory.setDbName("jetty_session");
                storeFactory.setCollectionName("jetty_session_collection");
                storeFactory.setGracePeriodSec(3600);
                server.addBean(storeFactory);

                DefaultSessionIdManager sessionIdManager = new DefaultSessionIdManager(server);
                sessionIdManager.setServer(server);
                sessionIdManager.setWorkerName("node1");
                SessionHandler handler = new SessionHandler();
                handler.setHttpOnly(true);
                handler.setMaxInactiveInterval(1800);
                handler.setSessionIdManager(sessionIdManager);
                server.setSessionIdManager(sessionIdManager);
                env.servlets().setSessionHandler(handler);
            }
        });
    }

pom.xml の記載

9.3.16.v20170120 から、9.4.2.v20170220 へ、jetty-nosql のversion を変更しました。
名前は変わったけど、artifactId はそのままのようです。

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-nosql</artifactId>
            <version>9.4.2.v20170220</version>
            <exclusions>
                <exclusion>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-server</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.eclipse.jetty.toolchain</groupId>
                    <artifactId>jetty-test-helper</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.mongodb</groupId>
                    <artifactId>mongo-java-driver</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.eclipse.jetty.tests</groupId>
                    <artifactId>test-sessions-common</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-jmx</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.google.guava</groupId>
                    <artifactId>guava</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

以上です。

コメント