View Javadoc
1   /*
2    * Copyright 2012-2019 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.es.config.exbhv;
17  
18  import java.util.regex.Pattern;
19  
20  import org.apache.commons.lang3.RandomUtils;
21  import org.codelibs.fess.es.config.bsbhv.BsScheduledJobBhv;
22  import org.codelibs.fess.es.config.exentity.ScheduledJob;
23  import org.codelibs.fess.util.ComponentUtil;
24  import org.dbflute.optional.OptionalEntity;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  /**
29   * @author FreeGen
30   */
31  public class ScheduledJobBhv extends BsScheduledJobBhv {
32  
33      private static final Logger logger = LoggerFactory.getLogger(ScheduledJobBhv.class);
34  
35      private String indexName = null;
36  
37      @Override
38      protected String asEsIndex() {
39          if (indexName == null) {
40              final String name = ComponentUtil.getFessConfig().getIndexConfigIndex();
41              indexName = super.asEsIndex().replaceFirst(Pattern.quote(".fess_config"), name);
42          }
43          return indexName;
44      }
45  
46      @Override
47      public OptionalEntity<ScheduledJob> selectByPK(final String id) {
48          Exception lastException = null;
49          for (int i = 0; i < 30; i++) {
50              try {
51                  return super.selectByPK(id);
52              } catch (final Exception e) {
53                  if (logger.isDebugEnabled()) {
54                      logger.debug("Failed to select a job by " + id, e);
55                  }
56                  lastException = e;
57                  try {
58                      Thread.sleep(RandomUtils.nextLong(500, 5000));
59                  } catch (final InterruptedException e1) {
60                      // ignore
61                  }
62              }
63          }
64          logger.warn("Failed to select a job by " + id, lastException);
65          return OptionalEntity.empty();
66      }
67  }