Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #100 from iron-io/worker-helper

[WIP] Change java example: now with WorkerHelper
commit 88bd2da673e63bedbc918bfdb8e7ef96272b53cd 2 parents 5eab341 + d884d9b
@treeder treeder authored
View
17 java/worker101/PayloadData.java
@@ -0,0 +1,17 @@
+import com.google.gson.annotations.SerializedName;
+
+class PayloadData {
+ @SerializedName("query_string")
+ String query;
+
+ public PayloadData() {
+ }
+
+ public String getQuery() {
+ return query;
+ }
+
+ public void setQuery(String query) {
+ this.query = query;
+ }
+}
View
8 java/worker101/README.md
@@ -4,13 +4,13 @@ This covers most of the core concepts of using IronWorker including loading thir
dependencies.
1. Be sure you've setup your Iron.io credentials, see main [README.md](https://github.com/iron-io/iron_worker_examples).
-1. Compile java file 'javac -cp "json-java.jar:gson-1.7.jar" Worker101.java'
-1. Make jar file 'jar cfm worker101.jar manifest.txt Worker101.class'
+1. Compile java file 'javac -cp "json-java.jar:gson-2.2.4.jar:ironworker.jar" Worker101.java PayloadData.java'
+1. Make jar file 'jar cfm worker101.jar manifest.txt Worker101.class PayloadData.class'
1. Run `iron_worker upload worker101` to upload the worker code package to IronWorker.
1. Queue up a task:
- 1. From command line: `iron_worker queue JavaWorker101 --payload '{"query":"xbox"}' --priority 2 --timeout 60`
+ 1. From command line: `iron_worker queue JavaWorker101 --payload '{"query_string":"xbox"}' --priority 2 --timeout 60`
1. With enqueue.java - get latest iron_worker_java lib (https://github.com/iron-io/iron_worker_java), change query, compile it and launch.
1. Look at [HUD](https://hud.iron.io) to view your tasks running, check logs, etc.
1. Schedule a task:
- 1. From command line: `iron_worker schedule JavaWorker101 --payload '{"query":"heyaa"}' --delay 5 --timeout 60 --start-at "12:30" --run-times 5 --run-every 70`
+ 1. From command line: `iron_worker schedule JavaWorker101 --payload '{"query_string":"heyaa"}' --delay 5 --timeout 60 --start-at "12:30" --run-times 5 --run-every 70`
View
72 java/worker101/Worker101.java
@@ -9,6 +9,7 @@
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONObject;
+import io.iron.ironworker.client.helpers.WorkerHelper;
public class Worker101 {
@@ -16,18 +17,32 @@
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
+ WorkerHelper helper = WorkerHelper.fromArgs(args);
System.out.println("Running worker");
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
String urlstr = "http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&rvsection=0&format=json&titles=";
- StringBuffer buff = new StringBuffer();
- urlstr += parse_payload(args);
+ // First way
+ // Edit PayloadData.java file according to structure of your payload
+ PayloadData payload = helper.getPayload(PayloadData.class);
+ urlstr += payload.getQuery();
+
+ // Second way
+ // For more info about JsonObject look at
+ // http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/JsonObject.html
+ // PayloadData payload = helper.getPayloadJson();
+ // urlstr += payload.get("query_string").getAsString();
+
+ // Third way:
+ // String rawPayload = helper.getPayload();
+ // Parse, print or do anything you want with this string
URL url = new URL(urlstr);
BufferedReader br = new BufferedReader(
new InputStreamReader(
url.openConnection().getInputStream()));
+ StringBuffer buff = new StringBuffer();
int c;
while((c=br.read())!=-1)
{
@@ -42,59 +57,6 @@ public static void main(String[] args) throws Exception{
writeFile("output.txt", wikiRes.toString());
}
- private static String parse_payload(String[] args) {
- //obtain the filename from the passed arguments
- int payloadPos = -1;
- for(int i=0; i < args.length; i++) {
- if(args[i].equals("-payload")) {
- payloadPos = i + 1;
- break;
- }
- }
- if(payloadPos >= args.length) {
- System.err.println("Invalid payload argument.");
- System.exit(1);
- }
- if(payloadPos == -1) {
- System.err.println("No payload argument.");
- System.exit(1);
- }
-
- //read the contents of the file to a string
- String payload = "";
-
- try {
- payload = readFile(args[payloadPos]);
- } catch (IOException e) {
- System.err.println("IOException");
- System.exit(1);
- }
- String query = "iron.io";
- try {
- //parse the string as JSON
- Gson gson = new Gson();
- JsonParser parser = new JsonParser();
- JsonObject passed_args = parser.parse(payload).getAsJsonObject();
- query = gson.fromJson(passed_args.get("query"), String.class);
- System.out.println("Query from payload:" + query);
- } catch (IllegalStateException e) {
- System.err.println("Payload is empty");
- }
- return query;
- }
-
- private static String readFile(String path) throws IOException {
- FileInputStream stream = new FileInputStream(new File(path));
- try {
- FileChannel chan = stream.getChannel();
- MappedByteBuffer buf = chan.map(FileChannel.MapMode.READ_ONLY, 0, chan.size());
- return Charset.defaultCharset().decode(buf).toString();
- }
- finally {
- stream.close();
- }
- }
-
private static void writeFile(String path,String content) throws IOException {
Writer out = new OutputStreamWriter(new FileOutputStream(path));
try {
View
BIN  java/worker101/gson-1.7.jar
Binary file not shown
View
BIN  java/worker101/gson-2.2.4.jar
Binary file not shown
View
BIN  java/worker101/ironworker.jar
Binary file not shown
View
2  java/worker101/manifest.txt
@@ -1,4 +1,4 @@
Main-Class: Worker101
-Class-Path: json-java.jar gson-1.7.jar
+Class-Path: json-java.jar gson-2.2.4.jar ironworker.jar
View
3  java/worker101/worker101.worker
@@ -1,6 +1,7 @@
runtime 'java'
file 'json-java.jar'
-file 'gson-1.7.jar'
+file 'gson-2.2.4.jar'
+file 'ironworker.jar'
exec 'worker101.jar'
name 'JavaWorker101'
Please sign in to comment.
Something went wrong with that request. Please try again.