View output in Aws Lambda basic function - java

i'm trying to invode aws lambda function using Java in my Eclipse, if i pass input as any string, the function is calling successfully but, i'm seeing null as the output, but i'm expecing the input text itself as output.
Here is my code
package simpledynamodb;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class LambdaFunctionHandler implements RequestHandler<String, String> {
#Override
public String handleRequest(String input, Context context) {
context.getLogger().log("Input is working");
context.getLogger().log("Input: " + input);
// TODO: implement your handler
return null;
}
}
The output looks like
Uploading function code to dynamodbmaven...
Upload success. Function ARN: arn:aws:lambda:us-west-2:169456523019:function:dynamodbmaven
Invoking function...
==================== FUNCTION OUTPUT ====================
null
In my code i'm passing "Hello world" as input text, i'm suppose to see the passed string instead i'm getting null.
My handler class, everything seems to be fine. Here is the pic of my lambda configuration
Can you tell me where i'm wrong in this?.

I'm super late here but the reason you aren't seeing the output is because you are looking at the output in Eclipse - which only returns the result of your function invocation.
You should look at the Cloudwatch Logs for the Lambda to get the logs of the run.

It's printing null because that's what the function is returning.
I assume when you say you should be getting the input text as output, what you really mean is that you should be seeing in the input string in the logs. I'm guessing you have the logging configured such that those logs aren't printing. To get past the logging configuration issues just to see what your input string is, you might try using System.out instead of context.getLogger().log

Related

Method does nothing if called within an if, if called directly from on event it works

I`m building a discord bot with jda, made a method to use mXparser to get a math operation as an input from the chat e.g: /math 5+1
wrote everything to get the message, separate the arguments from the input on the chat, everything works until I put the code inside an IF statement that checks if it actually starts with "/math", the code inside it uses mXparser to calculate everything and send it back to chat.
Tried just about everything I could think of, taking all variables off the method, rewriting everything, I don`t get any errors either as stack trace or in the code editor, it just doesnt go through, tried just printing everything and it works fine as well, printing all the values on the console, everything seems to be right.
This part is where I check for the message, get the Strings and trim everything
public void onMessageReceived(MessageReceivedEvent event) {
this.messageReceived = event;
this.prefix = Main.prefix;
checkPrefix = messageReceived.getMessage().getContentRaw().split("\\s" + prefix);
mainArg = checkPrefix[0];
checkArgs = messageReceived.getMessage().getContentRaw().split(" ");
callAllCommands();
}
Here is the command to take the actual expression from the chat input calculate it and send it back.
private void mathCommand() {
mathexp = new Expression(checkArgs[1]);
messageReceived.getChannel().sendTyping().queue();
essageReceived.getChannel().sendMessage(Double.toString(mathexp.calculate())).queue();
}
This is inside the callAllCommands() method, that is how it is supposed to work, if the command on the chat is /math then the expression e.g: /math 1+1 it will send the result back, if I take off the IF statement it works just fine but then I can't check for the command. The other commands do work fine with the IF statement
if (mainArg.contentEquals(prefix + "math")) {
mathCommand();
}
I don't really get any errors, it just does not work, sorry if I missed something really simple, i`m not that experienced yet.

JMeter, postprocessing a sample

in my project i would want to implement a plugin for JMeter.
So currently I am stuck at sampler - postprocessing step.
#Override
public void postProcessSampler(HTTPSamplerBase sampler, SampleResult result)
{
super.postProcessSampler(sampler, result);
String postData = sampler.getPropertyAsString(HTTPSamplerBase.ARGUMENTS);
// ...
// apply some operations to postData
// ...
//
// try to write it back to sampler : approach1
// sampler.removeProperty(HTTPSamplerBase.ARGUMENTS);
// sampler.addNonEncodedArgument(HTTPSamplerBase.ARGUMENTS, postData, "");
// Fails
}
So at the postprocessing step i would like to change the request body, whcih is usually stored in HTTPSamplerBase.ARGUMENTS property. However, somehow i cannot set anything to this field. Redefining it with another string gives me a class cast error. If I try operating with strings, then i get invocation exception...
So my question is, what is the correct way to change the sampler's post body?
Regards and thank you
Try out HTTPSamplerBase.getArguments() function instead, example code:
sampler.getArguments().removeAllArguments();
sampler.addNonEncodedArgument("foo","bar","");
sampler.setPostBodyRaw(true);
Also be aware that for this form of post-processing you don't even need to come up with a plugin, all can be done via JSR223 PostProcessor and Groovy language. The above code should work just fine

What is the best way to process arguments passed to main method in java?

I wanted to know the best way to process arguments passed to the main method.
User pass the arguments from command line. i.e. I have a shell script which will invoke my java program. I am using this java program to invoke web service.
For Example,The format of the input is as follows
"Ram,ABC,XYZ,null,null,27-04-15" "Raj,EFG,DEF,null,null,25-04-15" "Kiran,IJK,LMN,null,null,20-04-15"
as you see, within each string there are different attribute values(comma separated). and each set of input is space separated. And web service provides two methods which are as follows.
public void processArg(name,addr1,addr2,info1,info2,dob){
}
public void processArg2(name,addr1,addr2){
}
here first method processArg will be used to submit data for each set. Once this method returns success then i need invoke second method processArg2 which will check the status of the submission i.e whether it is success or not.
What is the best way to achieve this? Please let me know if i am not clearly explained.
Thanks
You can use string tokenizer to solve this issue.Following code can solve you problem.
public static void main(String[] args){
for(int i=0;i<args.length;i++){
String dataString=args[i];
String[] splittedData=dataString.trim().split(",");
processArg(splittedData[0],splittedData[1],splittedData[2],splittedData[3],splittedData[4]);
processArg2(splittedData[0],splittedData[1],splittedData[2]);
}
}

Intercepting and parsing Logger or console output without disturbing it

I am having some trouble with Java. What I want is pretty simple,
I am developing a plugin for a mod of a well-known game, Minecraft, and I need to be able to parse every line of console output that comes through to STOUT.
I've looked around on the internet but I can't seem to find anything I can use to do this. I know that some console output is through the default Logger, and some is through system.out. I have a function, parseConsole(String line) that will parse the line and do something if it contains a word, etc etc. I want this function to be called for every line of output displayed on the console, but it must not interrupt the flow, as in every line will still be sent to the console after being parsed by my function.
you can use System.setOut() to replace the console PrintStream. you can replace this with an implementation which inspects the text on its way to the console.
here is an example of how you might filter:
public class MyFilterPrintStream extends PrintStream {
#Override public void print(String s) {
// ... process output string here ...
// pass along to actual console output
super.print(s);
}
}
// install my filter
System.setOut(new MyFilterPrintStream(System.out));

StrutsTestCase test and "error" returned from Struts2 action class

I'm trying to use StrutsTestCase for testing my Struts2 actions, but I'm getting always the "error" value back while executing the "execute()" method from the proxy. Here's the example:
public void testSpike() throws Exception{
request.addHeader("param1", "param");
ActionProxy proxy = getActionProxy("/action/to/test.action");
assertNotNull(proxy);
TestAction action = (TestAction) proxy.getAction();
assertNotNull(action);
String output = proxy.execute();
}
the output string is always "error". Is there a way to understand what happened there? The logs are not saying anything, and even trying to debug placing a breakpoint on the Action class doesn't help (the code never stops there).
Any suggestions?
Thanks
Roberto
Add a breakpoint at the line 'String output = proxy.execute();'. Execute your test in debug mode in your favorite IDE and step through the code to realize why execute() method returns always 'error'.

Categories

Resources