public MetaMessageSessionFactory(final MetaClientConfig metaClientConfig)
      throws MetaClientException {
    super();
    checkConfig(metaClientConfig);
    this.metaClientConfig = metaClientConfig;
    final ClientConfig clientConfig = new ClientConfig();
    clientConfig.setTcpNoDelay(false);
    clientConfig.setWireFormatType(new MetamorphosisWireFormatType());
    clientConfig.setMaxScheduleWrittenBytes(Runtime.getRuntime().maxMemory() / 3);
    try {
      remotingClient = new RemotingClientWrapper(RemotingFactory.connect(clientConfig));
    } catch (final NotifyRemotingException e) {
      throw new NetworkException("Create remoting client failed", e);
    }
    // 如果有设置,则使用设置的url并连接,否则使用zk发现服务器
    if (this.metaClientConfig.getServerUrl() != null) {
      connectServer(this.metaClientConfig);
    } else {
      initZooKeeper();
    }

    producerZooKeeper =
        new ProducerZooKeeper(metaZookeeper, remotingClient, zkClient, metaClientConfig);
    sessionIdGenerator = new IdGenerator();
    // modify by wuhua
    consumerZooKeeper = initConsumerZooKeeper(remotingClient, zkClient, zkConfig);
    zkClientChangedListeners.add(producerZooKeeper);
    zkClientChangedListeners.add(consumerZooKeeper);
    subscribeInfoManager = new SubscribeInfoManager();
    recoverManager = new RecoverStorageManager(this.metaClientConfig, subscribeInfoManager);
    shutdownHook =
        new Thread() {

          @Override
          public void run() {
            try {
              isHutdownHookCalled = true;
              MetaMessageSessionFactory.this.shutdown();
            } catch (final MetaClientException e) {
              log.error("关闭session factory失败", e);
            }
          }
        };
    Runtime.getRuntime().addShutdownHook(shutdownHook);
  }
Exemplo n.º 2
0
 private void initClient() throws NotifyRemotingException {
   clientConfig = new ClientConfig();
   clientConfig.setConnectTimeout(this.connectionTimeout);
   clientConfig.setWireFormatType(new RpcWireFormatType());
   client = RemotingFactory.connect(clientConfig);
 }