Skip to content

Commit

Permalink
#346 Async: implement simple and important JSON parameters for comman…
Browse files Browse the repository at this point in the history
…ds -switched to parameters, not JSON
  • Loading branch information
Igor Polevoy committed Jun 21, 2017
1 parent 9dc2817 commit 33c8abe
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions build_no_tests.sh
@@ -0,0 +1 @@
mvn clean install -Dmaven.test.skip=true
19 changes: 10 additions & 9 deletions javalite-async/src/main/java/org/javalite/async/Command.java
Expand Up @@ -40,7 +40,7 @@ public abstract class Command {


private String jmsMessageId; private String jmsMessageId;


private Map params = map("class", getClass().getSimpleName()); private Map<String, String> params = map("class", getClass().getSimpleName());


private static final XStream X_STREAM = new XStream(new CDATAXppDriver()); private static final XStream X_STREAM = new XStream(new CDATAXppDriver());


Expand Down Expand Up @@ -114,20 +114,21 @@ public void setJMSMessageID(String jmsMessageId) {
} }


/** /**
* Adds parameters and their values to reflect by the {@link #toJsonLog()} method. * Adds parameters and their values to reflect by the {@link #getParams()} method.
* *
* @param params important parameters of this command - tings you want to see in a log file. * @param namesAndValues important parameters of this command - tings you want to see in a log file.
*/ */
public void addJsonParams(Map params){ @SuppressWarnings("unchecked")
this.params.putAll(params); public void addParams(String ... namesAndValues){
this.params.putAll(map(namesAndValues));
} }


/** /**
* Used by writing returned value to the log. * Used to understand important values related to this command (in log files).
* *
* @return JSON representation of this command. * @return important values related to processing of this command.
*/ */
public String toJsonLog(){ public Map<String, String> getParams(){
return JsonHelper.toJsonString(params); return params;
} }
} }
Expand Up @@ -45,8 +45,8 @@ public void onMessage(Message message) {
} }
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
onCommand(command); onCommand(command);
command.toJsonLog();
LOGGER.info(JsonHelper.toJsonString(map("processed_millis", (System.currentTimeMillis() - start), "info", command.toJsonLog()))); LOGGER.info(JsonHelper.toJsonString(map("processed_millis", (System.currentTimeMillis() - start), "info", command.getParams())));
} catch (Exception e) { } catch (Exception e) {
throw new AsyncException("Failed to process command", e); throw new AsyncException("Failed to process command", e);
} }
Expand Down
10 changes: 10 additions & 0 deletions javalite-async/src/test/java/org/javalite/async/CommandSpec.java
Expand Up @@ -3,8 +3,10 @@
import org.junit.Test; import org.junit.Test;


import java.io.IOException; import java.io.IOException;
import java.util.Map;


import static org.javalite.test.jspec.JSpec.a; import static org.javalite.test.jspec.JSpec.a;
import static org.javalite.test.jspec.JSpec.the;


/** /**
* @author Igor Polevoy on 1/31/16. * @author Igor Polevoy on 1/31/16.
Expand All @@ -29,4 +31,12 @@ public void shouldSerializeDeserializeBinary() throws IOException {
a(helloCommand1.getMessage()).shouldBeEqual("Thanks for all the fish..."); a(helloCommand1.getMessage()).shouldBeEqual("Thanks for all the fish...");
a(helloCommand).shouldNotBeTheSameAs(helloCommand1); a(helloCommand).shouldNotBeTheSameAs(helloCommand1);
} }

@Test
public void shouldGenerateParams() throws IOException {
HelloCommand helloCommand = new HelloCommand("Thanks for all the fish...");
Map<String, String> params = helloCommand.getParams();
the(params.get("class")).shouldBeEqual("HelloCommand");
the(params.get("message")).shouldBeEqual("Thanks for all the fish...");
}
} }
Expand Up @@ -13,6 +13,7 @@ public class HelloCommand extends Command {


public HelloCommand(String message) { public HelloCommand(String message) {
this.message = message; this.message = message;
addParams("message", message);
} }


public HelloCommand() {} //necessary to provide public HelloCommand() {} //necessary to provide
Expand Down

0 comments on commit 33c8abe

Please sign in to comment.