Created
March 29, 2018 20:36
-
-
Save bbende/a190baf21573c36f99eba459ae84c0c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Licensed to the Apache Software Foundation (ASF) under one or more | |
* contributor license agreements. See the NOTICE file distributed with | |
* this work for additional information regarding copyright ownership. | |
* The ASF licenses this file to You under the Apache License, Version 2.0 | |
* (the "License"); you may not use this file except in compliance with | |
* the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package org.apache.nifi.reporting.ambari; | |
import org.apache.nifi.annotation.lifecycle.OnScheduled; | |
import org.apache.nifi.annotation.lifecycle.OnUnscheduled; | |
import org.apache.nifi.components.PropertyDescriptor; | |
import org.apache.nifi.processor.AbstractProcessor; | |
import org.apache.nifi.processor.ProcessContext; | |
import org.apache.nifi.processor.ProcessSession; | |
import org.apache.nifi.processor.exception.ProcessException; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.LinkedHashSet; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.atomic.AtomicReference; | |
public class UpdatingAllowableValues extends AbstractProcessor { | |
// How often to refresh values from external system | |
static final long REFRESH_MS = 60000; | |
private final AtomicReference<Set<String>> allowableVales = new AtomicReference<>(Collections.emptySet()); | |
private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); | |
@OnScheduled | |
public void onScheduled(final ProcessContext context) { | |
executorService.scheduleAtFixedRate(() -> { | |
// TODO replace with call to fetch new values from an external service | |
final Set<String> values = new LinkedHashSet<>(); | |
values.add("Choice #1"); | |
values.add("Choice #2"); | |
values.add("Choice #3"); | |
// set the new values into the holder to be used later | |
allowableVales.set(values); | |
}, REFRESH_MS, REFRESH_MS, TimeUnit.MILLISECONDS); | |
} | |
@OnUnscheduled | |
public void onUnscheduled(final ProcessContext context) { | |
try { | |
executorService.shutdownNow(); | |
} catch (Exception e) { | |
getLogger().error("Error shutting down allowable values refresh task", e); | |
} | |
} | |
@Override | |
protected List<PropertyDescriptor> getSupportedPropertyDescriptors() { | |
final List<PropertyDescriptor> properties = new ArrayList<>(); | |
// dynamically create a new descriptor using the values that were refreshed in the background | |
properties.add(new PropertyDescriptor.Builder() | |
.name("My Property") | |
.description("This is my property") | |
.allowableValues(allowableVales.get()) | |
.build()); | |
return properties; | |
} | |
@Override | |
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { | |
// TODO fill in processing | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment