| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 361525e commit 2bb8aa3
17 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,7 +29,19 @@ | |||
| 29 | 29 | <packaging>jar</packaging> | |
| 30 | 30 | <name>allocations</name> | |
| 31 | 31 | ||
| 32 | - <properties> | ||
| 33 | - <fully.qualified.main.class>com.github.chrishantha.sample.allocations.App</fully.qualified.main.class> | ||
| 34 | - </properties> | ||
| 32 | + <dependencies> | ||
| 33 | + <dependency> | ||
| 34 | + <groupId>com.github.chrishantha.sample</groupId> | ||
| 35 | + <artifactId>base</artifactId> | ||
| 36 | + </dependency> | ||
| 37 | + </dependencies> | ||
| 38 | + | ||
| 39 | + <build> | ||
| 40 | + <plugins> | ||
| 41 | + <plugin> | ||
| 42 | + <groupId>org.apache.maven.plugins</groupId> | ||
| 43 | + <artifactId>maven-shade-plugin</artifactId> | ||
| 44 | + </plugin> | ||
| 45 | + </plugins> | ||
| 46 | + </build> | ||
| 35 | 47 | </project> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,33 +15,14 @@ | |||
| 15 | 15 | */ | |
| 16 | 16 | package com.github.chrishantha.sample.allocations; | |
| 17 | 17 | ||
| 18 | - import com.beust.jcommander.JCommander; | ||
| 19 | 18 | import com.beust.jcommander.Parameter; | |
| 19 | + import com.github.chrishantha.sample.base.SampleApplication; | ||
| 20 | 20 | ||
| 21 | - public class App { | ||
| 21 | + public class AllocationsApplication implements SampleApplication { | ||
| 22 | 22 | ||
| 23 | 23 | @Parameter(names = "--max", description = "Max Numbers") | |
| 24 | 24 | private long max = 10_000_000L; | |
| 25 | 25 | ||
| 26 | - @Parameter(names = "--help", description = "Display Help", help = true) | ||
| 27 | - private boolean help; | ||
| 28 | - | ||
| 29 | - public static void main(String[] args) { | ||
| 30 | - App app = new App(); | ||
| 31 | - final JCommander jcmdr = new JCommander(app); | ||
| 32 | - jcmdr.setProgramName(App.class.getSimpleName()); | ||
| 33 | - jcmdr.parse(args); | ||
| 34 | - | ||
| 35 | - System.out.println(app); | ||
| 36 | - | ||
| 37 | - if (app.help) { | ||
| 38 | - jcmdr.usage(); | ||
| 39 | - return; | ||
| 40 | - } | ||
| 41 | - | ||
| 42 | - app.start(); | ||
| 43 | - } | ||
| 44 | - | ||
| 45 | 26 | boolean isPrime(Long n) { | |
| 46 | 27 | //check if n is a multiple of 2 | |
| 47 | 28 | if (n % 2 == 0) return false; | |
@@ -53,7 +34,8 @@ boolean isPrime(Long n) { | |||
| 53 | 34 | return true; | |
| 54 | 35 | } | |
| 55 | 36 | ||
| 56 | - private void start() { | ||
| 37 | + @Override | ||
| 38 | + public void start() { | ||
| 57 | 39 | Long primeCount = 0L; | |
| 58 | 40 | for (long i = 0; i < max; i++) { | |
| 59 | 41 | if (isPrime(i)) { | |
@@ -66,7 +48,7 @@ private void start() { | |||
| 66 | 48 | @Override | |
| 67 | 49 | public String toString() { | |
| 68 | 50 | StringBuilder builder = new StringBuilder(); | |
| 69 | - builder.append("App [max="); | ||
| 51 | + builder.append("AllocationsApplication [max="); | ||
| 70 | 52 | builder.append(max); | |
| 71 | 53 | builder.append("]"); | |
| 72 | 54 | return builder.toString(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + com.github.chrishantha.sample.allocations.AllocationsApplication | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,6 @@ | |||
| 1 | + Base module for Sample Applications | ||
| 2 | + =================================== | ||
| 3 | + | ||
| 4 | + This module helps to write sample applications quickly by implementing | ||
| 5 | + `com.github.chrishantha.sample.base.SampleApplication` interface and registering the implementation class as a service | ||
| 6 | + in `META-INF/services/com.github.chrishantha.sample.base.SampleApplication` | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,38 @@ | |||
| 1 | + <?xml version="1.0" encoding="utf-8"?> | ||
| 2 | + <!-- | ||
| 3 | + # Copyright 2018 M. Isuru Tharanga Chrishantha Perera | ||
| 4 | + # | ||
| 5 | + # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 6 | + # you may not use this file except in compliance with the License. | ||
| 7 | + # You may obtain a copy of the License at | ||
| 8 | + # | ||
| 9 | + # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | + # | ||
| 11 | + # Unless required by applicable law or agreed to in writing, software | ||
| 12 | + # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 13 | + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 14 | + # See the License for the specific language governing permissions and | ||
| 15 | + # limitations under the License. | ||
| 16 | + --> | ||
| 17 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 18 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 19 | + | ||
| 20 | + <parent> | ||
| 21 | + <groupId>com.github.chrishantha.sample</groupId> | ||
| 22 | + <artifactId>java-samples</artifactId> | ||
| 23 | + <version>0.0.2-SNAPSHOT</version> | ||
| 24 | + <relativePath>../pom.xml</relativePath> | ||
| 25 | + </parent> | ||
| 26 | + | ||
| 27 | + <modelVersion>4.0.0</modelVersion> | ||
| 28 | + <artifactId>base</artifactId> | ||
| 29 | + <packaging>jar</packaging> | ||
| 30 | + <name>base</name> | ||
| 31 | + | ||
| 32 | + <dependencies> | ||
| 33 | + <dependency> | ||
| 34 | + <groupId>com.beust</groupId> | ||
| 35 | + <artifactId>jcommander</artifactId> | ||
| 36 | + </dependency> | ||
| 37 | + </dependencies> | ||
| 38 | + </project> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,61 @@ | |||
| 1 | + /* | ||
| 2 | + * Copyright 2018 M. Isuru Tharanga Chrishantha Perera | ||
| 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, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + package com.github.chrishantha.sample.base; | ||
| 17 | + | ||
| 18 | + import com.beust.jcommander.JCommander; | ||
| 19 | + import com.beust.jcommander.Parameter; | ||
| 20 | + import com.beust.jcommander.ParameterException; | ||
| 21 | + | ||
| 22 | + import java.util.Iterator; | ||
| 23 | + import java.util.ServiceLoader; | ||
| 24 | + | ||
| 25 | + public class App { | ||
| 26 | + | ||
| 27 | + private static class CommonArgs { | ||
| 28 | + @Parameter(names = "--help", description = "Display Help", help = true) | ||
| 29 | + private boolean help; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + public static void main(String[] args) { | ||
| 33 | + // There should be only one application | ||
| 34 | + Iterator<SampleApplication> applicationIterator = ServiceLoader.load(SampleApplication.class).iterator(); | ||
| 35 | + if (!applicationIterator.hasNext()) { | ||
| 36 | + throw new IllegalStateException("Could not load Sample Application"); | ||
| 37 | + } | ||
| 38 | + SampleApplication sampleApplication = applicationIterator.next(); | ||
| 39 | + CommonArgs commonArgs = new CommonArgs(); | ||
| 40 | + final JCommander jcmdr = JCommander.newBuilder() | ||
| 41 | + .programName(sampleApplication.getClass().getSimpleName()) | ||
| 42 | + .addObject(sampleApplication) | ||
| 43 | + .addObject(commonArgs) | ||
| 44 | + .build(); | ||
| 45 | + | ||
| 46 | + try { | ||
| 47 | + jcmdr.parse(args); | ||
| 48 | + } catch (ParameterException ex) { | ||
| 49 | + System.err.println(ex.getMessage()); | ||
| 50 | + return; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + if (commonArgs.help) { | ||
| 54 | + jcmdr.usage(); | ||
| 55 | + return; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + System.out.println(sampleApplication); | ||
| 59 | + sampleApplication.start(); | ||
| 60 | + } | ||
| 61 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,23 @@ | |||
| 1 | + /* | ||
| 2 | + * Copyright 2018 M. Isuru Tharanga Chrishantha Perera | ||
| 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, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + package com.github.chrishantha.sample.base; | ||
| 17 | + | ||
| 18 | + public interface SampleApplication { | ||
| 19 | + | ||
| 20 | + void start(); | ||
| 21 | + | ||
| 22 | + String toString(); | ||
| 23 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,7 +29,19 @@ | |||
| 29 | 29 | <packaging>jar</packaging> | |
| 30 | 30 | <name>highcpu</name> | |
| 31 | 31 | ||
| 32 | - <properties> | ||
| 33 | - <fully.qualified.main.class>com.github.chrishantha.sample.highcpu.App</fully.qualified.main.class> | ||
| 34 | - </properties> | ||
| 32 | + <dependencies> | ||
| 33 | + <dependency> | ||
| 34 | + <groupId>com.github.chrishantha.sample</groupId> | ||
| 35 | + <artifactId>base</artifactId> | ||
| 36 | + </dependency> | ||
| 37 | + </dependencies> | ||
| 38 | + | ||
| 39 | + <build> | ||
| 40 | + <plugins> | ||
| 41 | + <plugin> | ||
| 42 | + <groupId>org.apache.maven.plugins</groupId> | ||
| 43 | + <artifactId>maven-shade-plugin</artifactId> | ||
| 44 | + </plugin> | ||
| 45 | + </plugins> | ||
| 46 | + </build> | ||
| 35 | 47 | </project> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,13 +15,13 @@ | |||
| 15 | 15 | */ | |
| 16 | 16 | package com.github.chrishantha.sample.highcpu; | |
| 17 | 17 | ||
| 18 | - import com.beust.jcommander.JCommander; | ||
| 19 | 18 | import com.beust.jcommander.Parameter; | |
| 19 | + import com.github.chrishantha.sample.base.SampleApplication; | ||
| 20 | 20 | ||
| 21 | 21 | import java.util.Timer; | |
| 22 | 22 | import java.util.TimerTask; | |
| 23 | 23 | ||
| 24 | - public class App { | ||
| 24 | + public class HighCpuApplication implements SampleApplication { | ||
| 25 | 25 | ||
| 26 | 26 | @Parameter(names = "--run-hashing", description = "Run Hashing Worker", arity = 1) | |
| 27 | 27 | private boolean runHashing = true; | |
@@ -47,29 +47,11 @@ public class App { | |||
| 47 | 47 | @Parameter(names = "--hashing-algo", description = "Hashing Algorithm") | |
| 48 | 48 | private String hashingAlgorithm = "SHA-1"; | |
| 49 | 49 | ||
| 50 | - @Parameter(names = "--help", description = "Display Help", help = true) | ||
| 51 | - private boolean help; | ||
| 52 | - | ||
| 53 | 50 | @Parameter(names = "--exit-timeout", description = "Exit Timeout in Seconds (Default 2 minutes. Use 0 to run forever)") | |
| 54 | 51 | private int exitTimeoutInSeconds = 2 * 60; | |
| 55 | 52 | ||
| 56 | - public static void main(String[] args) { | ||
| 57 | - App app = new App(); | ||
| 58 | - final JCommander jcmdr = new JCommander(app); | ||
| 59 | - jcmdr.setProgramName(App.class.getSimpleName()); | ||
| 60 | - jcmdr.parse(args); | ||
| 61 | - | ||
| 62 | - System.out.println(app); | ||
| 63 | - | ||
| 64 | - if (app.help) { | ||
| 65 | - jcmdr.usage(); | ||
| 66 | - return; | ||
| 67 | - } | ||
| 68 | - | ||
| 69 | - app.start(); | ||
| 70 | - } | ||
| 71 | - | ||
| 72 | - private void start() { | ||
| 53 | + @Override | ||
| 54 | + public void start() { | ||
| 73 | 55 | System.out.println("Starting Application..."); | |
| 74 | 56 | if (exitTimeoutInSeconds > 0) { | |
| 75 | 57 | final Timer timer = new Timer(); | |
@@ -106,7 +88,7 @@ private void startThread(int i, String name, Runnable worker) { | |||
| 106 | 88 | @Override | |
| 107 | 89 | public String toString() { | |
| 108 | 90 | StringBuilder builder = new StringBuilder(); | |
| 109 | - builder.append("App [runHashing="); | ||
| 91 | + builder.append("HighCpuApplication [runHashing="); | ||
| 110 | 92 | builder.append(runHashing); | |
| 111 | 93 | builder.append(", runSleeping="); | |
| 112 | 94 | builder.append(runSleeping); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + com.github.chrishantha.sample.highcpu.HighCpuApplication | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments