在 Vert.x 中正确使用 MongoClient 的模式 [英] Pattern for using properly MongoClient in Vert.x

查看:34
本文介绍了在 Vert.x 中正确使用 MongoClient 的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 MongoClient 类感到很不舒服,当然是因为我不完全理解它是什么以及它是如何工作的.

I feel quite uncomfortable with the MongoClient class, certainly because I don't exactly understand what it is and how it works.

MongoClient.createShared 的第一次调用实际上会创建池,将使用指定的配置.

The first call to MongoClient.createShared will actually create the pool, and the specified config will be used.

后续调用将返回一个新的客户端实例,该实例使用相同的pool,因此不会使用该配置.

Subsequent calls will return a new client instance that uses the same pool, so the configuration won’t be used.

这是否意味着模式应该是:

Does that mean that the pattern should be:

  1. 在启动函数中,为了创建池,我们调用

  1. In startup function, to create the pool, we make a call

mc = MongoClient.createShared(vx, config, "poolname");

如果第一次调用成功,返回值 mc 是否重要?如果池创建失败,它的价值是什么?文件没有说.如果 mongod 未运行,则会出现套接字异常,但其他情况呢?

Is the returned value mc important for this first call if it succeeds? What is its value if the creation of the pool fails? The documentations doesn't say. There is a socket exception if mongod is not running, but what about the other cases?

在代码的另一个地方(比如另一个verticle),我们能不能写mc = MongoClient.createShared(vx, new JsonObject(), "poolname"); 来避免系统地需要访问共享对象.

In another place in the code (another verticle, for example), can we write mc = MongoClient.createShared(vx, new JsonObject(), "poolname"); to avoid to systematically need to access shared objects.

再次,在另一个需要访问数据库的verticle中,我们是否应该定义MongoClient mc

Again, In another verticle where we need to access the database, should we define MongoClient mc

  • 作为类字段,在这种情况下,它只会在 stop() 方法中释放到池中,或者
  • 它不应该是一个用 MongoClient.createShared(...) 填充并在我们不需要时用 mc.close() 取消分配的变量连接是否再次释放到池中?
  • as a class field in which case it will be released to the pool only in the stop() method, or
  • shouldn't it be a variable populated with MongoClient.createShared(...) and de-allocated with mc.close() once we don't need the connection any more in order release it again to the pool ?

我会写如下

// Main startup Verticle
import ...

public class MainVerticle extends AbstractVerticle {
  ...      
  @Override
  public void start(Future<Void> sf) throws Exception {
     ...
     try {
       MongoClient.createShared(vx, config().getJsonObject("mgcnf"),  "pool");
     }
     catch(Exception e) {
       log.error("error error...");
       sf.fail("failure reason");
       return;
     }
     ...
     sf.complete();
  }
  ...some other methods
}

然后,在其他地方

public class SomeVerticle extends AbstractVerticle {

    public void someMethod(...) {
    ...
    // use the database:
    MongoClient mc = MongoClient.createShared(vx,  new JsonObject(),  "pool");
        mc.save(the_coll, the_doc, res -> {
            mc.close();
            if(res.succeeded()) {
                ...
            }
            else {
                ...
            }   
       }
       ...
   }
   ...
}

这样有意义吗?然而,这不是我在互联网上可以找到的示例中的内容.

Does that make sense ? Yet, this is not what is in the examples that I could find around the internet.

推荐答案

不要担心池.不要使用它们.他们不会做你认为他们会做的事情.

Don't worry about pools. Don't use them. They don't do what you think they do.

在任何 Verticle 的 start 方法中,在 AbstractVerticle 的继承者上设置一个字段(您称之为类字段,但实际上是指实例字段)为 <代码>MongoClient.createShared(getVertx(), config).在您的 stop 方法中关闭客户端.就是这样.

In your start method of any verticle, set a field (what you call a class field, but you really mean instance field) on the inheritor of AbstractVerticle to MongoClient.createShared(getVertx(), config). Close the client in your stop method. That's it.

您将看到的其他例外情况是:

The other exceptions you'll see are:

  • 错误的用户名/密码
  • 集群状态不佳
  • Java 驱动程序的连接数限制为 500 或 1,000 个(取决于版本),如果超过此连接数,您将收到异常

两者都将从包装在 VertxException 中的驱动程序向上传播.

Both will be propagated up from the driver wrapped in a VertxException.

这篇关于在 Vert.x 中正确使用 MongoClient 的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆