Skip to content

Commit

Permalink
add fluo exec command
Browse files Browse the repository at this point in the history
  • Loading branch information
keith-turner committed Dec 11, 2015
1 parent b16774a commit 586561b
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 2 deletions.
27 changes: 27 additions & 0 deletions docs/applications.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@ FluoClient client = FluoFactory.newClient(config)

It may help to reference the [API javadocs][API] while you are learning the Fluo API.

Running application code
------------------------

The `fluo exec <app name> <class> {arguments}` provides an easy way to execute
application code. It will execute a class with a main method if a jar
containing the class is placed in the lib directory of the application. When
the class is run, Fluo classes and dependencies will be on the classpath. The
`fluo exec` command can inject the applications configuration if the class is
written in the following way. Defining the injection point is optional.

```java
import javax.inject.Inject;

public class AppCommand {

//when run with fluo exec command, the applications configuration will be injected
@Inject
private static FluoConfiguration fluoConfig;

public static void main(String[] args) throws Exception {
try(FluoClient fluoClient = FluoFactory.newClient(fluoConfig)) {
//do stuff with Fluo
}
}
}
```

Creating a Fluo observer
------------------------

Expand Down
5 changes: 5 additions & 0 deletions modules/cluster/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public static void main(String[] args) {
verifyNoArgs(remainArgs);
runner.waitUntilFinished(fluoInstall.resolveFluoConfiguration(appName));
break;
case "exec":
runner.exec(fluoInstall.resolveFluoConfiguration(appName), remainArgs);
break;
default:
System.err.println("Unknown command: " + command);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static FluoConfiguration chooseConfig(String appName) {
return chosenConfig;
}

public static void main(String[] args) {
public static void main(String[] args) throws Exception {

if (args.length < 3) {
System.err.println("ERROR - Expected at least two arguments. "
Expand Down Expand Up @@ -111,6 +111,9 @@ public static void main(String[] args) {
verifyNoArgs(remainArgs);
runner.waitUntilFinished(chooseConfig(appName));
break;
case "exec":
runner.exec(chooseConfig(appName), remainArgs);
break;
default:
System.err.println("Unknown command: " + command);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
package io.fluo.cluster.runner;

import java.io.File;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
import com.google.common.annotations.VisibleForTesting;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Provider;
import io.fluo.api.client.FluoClient;
import io.fluo.api.client.FluoFactory;
import io.fluo.api.client.Snapshot;
Expand Down Expand Up @@ -307,4 +311,42 @@ public void waitUntilFinished(FluoConfiguration config) {
System.exit(-1);
}
}

private static class FluoConfigModule extends AbstractModule {

private Class<?> clazz;
private FluoConfiguration fluoConfig;

FluoConfigModule(Class<?> clazz, FluoConfiguration fluoConfig) {
this.clazz = clazz;
this.fluoConfig = fluoConfig;
}

@Override
protected void configure() {
requestStaticInjection(clazz);
bind(FluoConfiguration.class).toProvider(new Provider<FluoConfiguration>() {
@Override
public FluoConfiguration get() {
// TODO Auto-generated method stub
return fluoConfig;
}
});
}

}

public void exec(FluoConfiguration fluoConfig, String[] args) throws Exception {

String className = args[0];
Arrays.copyOfRange(args, 1, args.length);

Class<?> clazz = Class.forName(className);

// inject fluo configuration
Guice.createInjector(new FluoConfigModule(clazz, fluoConfig));

Method method = clazz.getMethod("main", String[].class);
method.invoke(null, (Object) Arrays.copyOfRange(args, 1, args.length));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class AccumuloUtil {
*/
public static Instance getInstance(FluoConfiguration config) {
ClientConfiguration clientConfig =
ClientConfiguration.loadDefault().withInstance(config.getAccumuloInstance())
new ClientConfiguration().withInstance(config.getAccumuloInstance())
.withZkHosts(config.getAccumuloZookeepers())
.withZkTimeout(config.getZookeeperTimeout() / 1000);
return new ZooKeeperInstance(clientConfig);
Expand Down
4 changes: 4 additions & 0 deletions modules/distribution/src/main/scripts/fluo
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function print_usage {
echo " scan <app> Prints snapshot of data in Fluo application"
echo " classpath Prints the classpath needed for fluo clients. Run with -h to learn more."
echo " wait <app> Waits until all notifications are processed"
echo " exec <app> <class> {<argument>}";
echo " "
exit 1
}
Expand Down Expand Up @@ -140,6 +141,9 @@ list)
classpath)
java -cp "$FLUO_LIB_DIR/*:$FLUO_LIB_DIR/logback/*" io.fluo.cluster.command.FluoCommand $FLUO_HOME none $1 app ${*:2}
;;
exec)
java -cp "$FLUO_LIB_DIR/*:$FLUO_LIB_DIR/logback/*:$APP_LIB_DIR/*" io.fluo.cluster.command.FluoCommand $FLUO_HOME $HADOOP_PREFIX "$@"
;;
*)
print_usage
esac
5 changes: 5 additions & 0 deletions modules/distribution/src/main/scripts/mini-fluo
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function print_usage {
echo " scan <app> Prints snapshot of data in MiniFluo application"
echo " classpath Prints the classpath needed for fluo clients. Run with -h to learn more."
echo " wait <app> Waits until all notifications are processed"
echo " exec <app> <class> {<argument>}";
echo " "
exit 1
}
Expand Down Expand Up @@ -152,6 +153,10 @@ scan|wait)
classpath)
java -cp "$FLUO_LIB_DIR/*:$FLUO_LIB_DIR/logback/*:$APP_LIB_DIR/*" io.fluo.cluster.command.MiniFluoCommand $FLUO_HOME $1 app ${*:2}
;;
exec)
java -cp "$FLUO_LIB_DIR/*:$FLUO_LIB_DIR/logback/*:$APP_LIB_DIR/*" io.fluo.cluster.command.MiniFluoCommand $FLUO_HOME "$@"
;;

*)
print_usage
esac

0 comments on commit 586561b

Please sign in to comment.