Created
August 18, 2012 18:28
-
-
Save etoews/3388897 to your computer and use it in GitHub Desktop.
Logging with jclouds
This file contains hidden or 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
import java.lang.Thread.UncaughtExceptionHandler; | |
import java.util.Set; | |
import org.jclouds.ContextBuilder; | |
import org.jclouds.compute.ComputeService; | |
import org.jclouds.compute.ComputeServiceContext; | |
import org.jclouds.compute.domain.ComputeMetadata; | |
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule; | |
import com.google.common.collect.ImmutableSet; | |
import com.google.inject.Module; | |
public class JCloudsLoggingExample { | |
private ComputeService compute; | |
public static void main(String[] args) { | |
JCloudsLoggingExample jCloudsLoggingExample = new JCloudsLoggingExample(); | |
jCloudsLoggingExample.init(); | |
jCloudsLoggingExample.listServers(); | |
jCloudsLoggingExample.close(); | |
} | |
private void listServers() { | |
System.out.println("Calling listNodes..."); | |
Set<? extends ComputeMetadata> nodes = compute.listNodes(); | |
System.out.println("Total Number of Nodes = " + nodes.size()); | |
for (ComputeMetadata node: nodes) { | |
System.out.println("\t" + node); | |
} | |
} | |
private void init() { | |
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { | |
public void uncaughtException(Thread t, Throwable e) { | |
if (compute != null) close(); | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
}); | |
Iterable<Module> modules = ImmutableSet.<Module> of( | |
new SLF4JLoggingModule()); | |
String provider = "rackspace-cloudservers-us"; | |
String identity = "my-username"; | |
String apiKey = "my-api-key"; | |
ComputeServiceContext context = ContextBuilder.newBuilder(provider) | |
.credentials(identity, apiKey) | |
.modules(modules) | |
.buildView(ComputeServiceContext.class); | |
compute = context.getComputeService(); | |
} | |
private void close() { | |
compute.getContext().close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment