001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.component.file.cluster; 018 019import java.util.concurrent.ScheduledExecutorService; 020import java.util.concurrent.TimeUnit; 021 022import org.apache.camel.CamelContext; 023import org.apache.camel.impl.cluster.AbstractCamelClusterService; 024import org.apache.camel.util.ObjectHelper; 025 026public class FileLockClusterService extends AbstractCamelClusterService<FileLockClusterView> { 027 private String root; 028 private long acquireLockDelay; 029 private TimeUnit acquireLockDelayUnit; 030 private long acquireLockInterval; 031 private TimeUnit acquireLockIntervalUnit; 032 private ScheduledExecutorService executor; 033 034 public FileLockClusterService() { 035 this.acquireLockDelay = 1; 036 this.acquireLockDelayUnit = TimeUnit.SECONDS; 037 this.acquireLockInterval = 10; 038 this.acquireLockIntervalUnit = TimeUnit.SECONDS; 039 } 040 041 @Override 042 protected FileLockClusterView createView(String namespace) throws Exception { 043 return new FileLockClusterView(this, namespace); 044 } 045 046 public String getRoot() { 047 return root; 048 } 049 050 /** 051 * Sets the root path. 052 */ 053 public void setRoot(String root) { 054 this.root = root; 055 } 056 057 public long getAcquireLockDelay() { 058 return acquireLockDelay; 059 } 060 061 /** 062 * The time to wait before starting to try to acquire lock, default 1. 063 */ 064 public void setAcquireLockDelay(long acquireLockDelay) { 065 this.acquireLockDelay = acquireLockDelay; 066 } 067 068 public void setAcquireLockDelay(long pollDelay, TimeUnit pollDelayUnit) { 069 setAcquireLockDelay(pollDelay); 070 setAcquireLockDelayUnit(pollDelayUnit); 071 } 072 073 public TimeUnit getAcquireLockDelayUnit() { 074 return acquireLockDelayUnit; 075 } 076 077 /** 078 * The time unit fo the acquireLockDelay, default to TimeUnit.SECONDS. 079 */ 080 public void setAcquireLockDelayUnit(TimeUnit acquireLockDelayUnit) { 081 this.acquireLockDelayUnit = acquireLockDelayUnit; 082 } 083 084 public long getAcquireLockInterval() { 085 return acquireLockInterval; 086 } 087 088 /** 089 * The time to wait between attempts to try to acquire lock, default 10. 090 */ 091 public void setAcquireLockInterval(long acquireLockInterval) { 092 this.acquireLockInterval = acquireLockInterval; 093 } 094 095 public void setAcquireLockInterval(long pollInterval, TimeUnit pollIntervalUnit) { 096 setAcquireLockInterval(pollInterval); 097 setAcquireLockIntervalUnit(pollIntervalUnit); 098 } 099 100 public TimeUnit getAcquireLockIntervalUnit() { 101 return acquireLockIntervalUnit; 102 } 103 104 /** 105 * The time unit fo the acquireLockInterva, default to TimeUnit.SECONDS. 106 */ 107 public void setAcquireLockIntervalUnit(TimeUnit acquireLockIntervalUnit) { 108 this.acquireLockIntervalUnit = acquireLockIntervalUnit; 109 } 110 111 @Override 112 protected void doStop() throws Exception { 113 super.doStop(); 114 115 CamelContext context = getCamelContext(); 116 117 if (executor != null) { 118 if (context != null) { 119 context.getExecutorServiceManager().shutdown(executor); 120 } else { 121 executor.shutdown(); 122 } 123 124 executor = null; 125 } 126 } 127 128 synchronized ScheduledExecutorService getExecutor() { 129 if (executor == null) { 130 // Camel context should be set at this stage. 131 final CamelContext context = ObjectHelper.notNull(getCamelContext(), "CamelContext"); 132 133 executor = context.getExecutorServiceManager().newSingleThreadScheduledExecutor(this, "FileLockClusterService-" + getId()); 134 } 135 136 return executor; 137 } 138}