@@ -0,0 +1,3346 @@
+/*%0A * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0Aimport com.hazelcast.jet.Jet;%0Aimport com.hazelcast.jet.JetInstance;%0Aimport com.hazelcast.jet.Job;%0Aimport com.hazelcast.jet.JobAlreadyExistsException;%0Aimport com.hazelcast.jet.config.JetConfig;%0Aimport com.hazelcast.jet.config.JobConfig;%0Aimport com.hazelcast.jet.pipeline.Pipeline;%0Aimport com.hazelcast.jet.pipeline.Sources;%0A%0Aimport java.util.concurrent.CancellationException;%0A%0Aimport static com.hazelcast.jet.pipeline.JournalInitialPosition.START_FROM_OLDEST;%0Aimport static com.hazelcast.jet.pipeline.Sinks.list;%0A%0A/**%0A * We demonstrate how just one running instance of a named job can be created%0A * in the cluster with multiple submissions. The first submission call creates%0A * the job and the other submission calls will return the same job.%0A * This behavior is useful for micro-service deployments where Jet is used%0A * in the embedded mode. Users deploy the same self-contained package into%0A * multiple instances and each instance submits the same job during startup.%0A */%0Apublic class ExclusiveJobExecution %7B%0A%0A    public static void main(String%5B%5D args) %7B%0A        System.setProperty(%22hazelcast.logging.type%22, %22log4j%22);%0A%0A        JetConfig config = new JetConfig();%0A        config.getHazelcastConfig().getMapEventJournalConfig(%22source%22).setEnabled(true);%0A        JetInstance instance1 = Jet.newJetInstance(config);%0A        JetInstance instance2 = Jet.newJetInstance(config);%0A%0A        Pipeline p = Pipeline.create();%0A        p.drawFrom(Sources.%3CInteger, Integer%3EmapJournal(%22source%22, START_FROM_OLDEST))%0A                .withoutTimestamps()%0A                .drainTo(list(%22sink%22));%0A%0A        JobConfig jobConfig = new JobConfig().setName(%22namedJob%22);%0A%0A        // Submit the same job from multiple Jet instances%0A        Job job1 = instance1.newJobIfAbsent(p, jobConfig);%0A        Job job2 = instance2.newJobIfAbsent(p, jobConfig);%0A%0A        // Both Job handles refer to the same job%0A        assert job1.getId() == job2.getId();%0A%0A        try %7B%0A            // If I try to submit the same job with newJob(), I will get an exception%0A            instance1.newJob(p, jobConfig);%0A            assert false;%0A        %7D catch (JobAlreadyExistsException ignored) %7B%0A        %7D%0A%0A        job1.cancel();%0A%0A        try %7B%0A            job1.join();%0A        %7D catch (CancellationException ignored) %7B%0A        %7D%0A%0A        // Now, I can submit a new job with the same name because there is no active job with that name%0A        Job job3 = instance1.newJobIfAbsent(p, jobConfig);%0A%0A        // This is a new job instance%0A        assert job3.getId() != job1.getId();%0A%0A        job3.cancel();%0A        try %7B%0A            job3.join();%0A        %7D catch (CancellationException ignored) %7B%0A        %7D%0A%0A        instance1.shutdown();%0A        instance2.shutdown();%0A    %7D%0A%0A%7D%0A

Add a new code sample for the exclusive job execution feature (#171)