AWS Lambda : How to call lambda function from simple java class - java

I have created simple Lambda function and upload this to AWS Lambda.
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class Hello implements RequestHandler<String, String> {
#Override
public String handleRequest(String input, Context context) {
String output = "Bonjour, " + input + "!";
return output;
}
}
}
I want to invoke this Lambda function from some other project using java class. I am using aws-java-sdk-lambda-1.10.22 to call the function. But I am not able to succeed in that.
Here is my InvokeLambda class which is a separate project.
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.lambda.AWSLambdaClient;
import com.amazonaws.services.lambda.model.InvokeRequest;
public class InvokeLambda {
private static final Log logger = LogFactory.getLog(InvokeLambda.class);
private static final String awsAccessKeyId = "XXXXXX";
private static final String awsSecretAccessKey = "YYYY";
private static final String regionName = "us-west-2";
private static final String functionName = "Hello";
private static Region region;
private static AWSCredentials credentials;
private static AWSLambdaClient lambdaClient;
/**
* The entry point into the AWS lambda function.
*/
public static void main(String... args) {
credentials = new BasicAWSCredentials(awsAccessKeyId,
awsSecretAccessKey);
lambdaClient = (credentials == null) ? new AWSLambdaClient()
: new AWSLambdaClient(credentials);
//lambdaClient.configureRegion(Regions.US_WEST_2);
region = Region.getRegion(Regions.fromName(regionName));
lambdaClient.setRegion(region);
try {
InvokeRequest invokeRequest = new InvokeRequest();
invokeRequest.setFunctionName(functionName);
invokeRequest.setPayload("\" AWS Lambda\"");
System.out.println(byteBufferToString(
lambdaClient.invoke(invokeRequest).getPayload(),
Charset.forName("UTF-8")));
} catch (Exception e) {
logger.error(e.getMessage());
// System.out.println(e.getMessage());
}
}
public static String byteBufferToString(ByteBuffer buffer, Charset charset) {
byte[] bytes;
if (buffer.hasArray()) {
bytes = buffer.array();
} else {
bytes = new byte[buffer.remaining()];
buffer.get(bytes);
}
return new String(bytes, charset);
}
}
How to call the lambda function using java?

Given the information in your comment, your client code to invoke the function is fine. The problem appears to be with the configuration of the function itself. Specifically, AWS Lambda is not able to find the handler you've specified (com.aws.HelloLambda::handleRequest) because that doesn't match the name and package of your handler class (Hello) and the name of your method in that class (handleRequest).
You can update the function handler name through the AWS Console. Choose your function, then the configuration tab and then the Handler property.
You probably want to change it from com.aws.HelloLambda::handleRequest to Hello::handleRequest.
Before testing the function from your Java client, you could test it directly through the console, this will help you ensure the function is configured correctly.

Another way to invoke lambda from java code is to use LambdaInvokerFactory and I found this approach cleaner.
You need to do the following:
Define interface representing your function and annotate method with #LambdaFunction
Create implementation of the above interface using LambdaInvokerFactory
Invoke lambda function using just created proxy object (interface implementation)
More detailed example can be found here.

Related

Calling Java class in another class not working for me [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
I thought when Java classes are in the same directory, you don't need to import another class when you use it inside another.
I have this class that will initiate Cloudinary uploading of files back to the cloud, but when I call it inside another class and run build I get this error 'cannot access CloudinaryUpload'
The Cloudinary class
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.cloudinary.Cloudinary;
import com.cloudinary.utils.ObjectUtils;
public class CloudinaryUpload {
public static void main(String arg[])throws Exception{
Map config = ObjectUtils.asMap(
"cloud_name", "name",
"api_key", "api_key",
"api_secret", "api_secret",
"secure", true
);
Cloudinary cloudinary = new Cloudinary(config);
}
}
Just a snippet of code of another class where I am calling it
public class ClientManagerServices {
private static final int BYTES_DOWNLOAD = 1024;
//The Cloudinary class
private CloudinaryUpload cloudinaryUpload = CloudinaryUpload();
public static String getMessageBody(Delegator delegator, String requester, String subject, String registryFileId, String clientId) {
GenericValue fileData = null;
GenericValue userData = null;
GenericValue clientData = null;
String bodyToReturn = "";
try {
fileData = delegator.findOne("RegistryFile", UtilMisc.toMap("registryFileId", registryFileId), false);
} catch (GenericEntityException e) {
e.printStackTrace();
}
}
This line:
private CloudinaryUpload cloudinaryUpload = CloudinaryUpload();
If that is supposed to creating new instance it should be
private CloudinaryUpload cloudinaryUpload = new CloudinaryUpload();
Note ... new. If you leave out the new, then that is a call to a method named CloudinaryUpload ... and no such method exists in your code.

Java ClassLoading from client's memory

I am developing a client-server application using Java Sockets where at some point the client has to download some classes from the server (through the already bound Socket) and use them. I am able to read the class file bytes and send them to the client. So then, the client has to use a ClassLoader to load the classes from memory. So, in my opinion, the problem is not really related to Sockets, but it is about custom class loading (you will see why I say that, later in this post).
My setup is as follows: the client project with a single package named client with a single class in it, named LiveClientTest and then the server project with a single package named server with 3 classes in it: ClientMain, LiveServerTest (the entry point) and MyStrings. In short, all server-side code is under the package server and all client-side under the client. Each of the two packages is in a different project also.
The problem occurs when the custom ClassLoader of the client (named LiveClientTest.MemoryClassLoader) tries to load a non-static nested class (named MyStrings.NestedNonStaticSubclass) which refers to its enclosing class (named MyStrings) before constructing the object (of type MyStrings.NestedNonStaticSubclass). Although the classes compile fine, the error appears at runtime, while loading the class MyStrings.NestedNonStaticSubclass with the custom ClassLoader. I know it sounds weird, but you can see what I mean if you take a look at the code.
Server side code:
LiveServerTest class (entry point):
package server;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import javax.swing.JOptionPane;
public class LiveServerTest {
//Convert class binary name representation to file path:
public static String classNameToResourcePath(final String className) {
return className.replace('.', File.separatorChar) + ".class";
}
//Get directory location of the given class (packages not included):
public static Path getDirectoyPath(final Class clazz) throws URISyntaxException {
return new File(clazz.getProtectionDomain().getCodeSource().getLocation().toURI()).toPath();
}
//Get absolute file location of the given class:
public static Path getClassFilePath(final Class c) throws URISyntaxException {
return getDirectoyPath(c).resolve(classNameToResourcePath(c.getName()));
}
public static void main(final String[] args) {
ServerSocket srv = null;
try {
srv = new ServerSocket(Integer.parseInt(Objects.requireNonNull(JOptionPane.showInputDialog(null, "Enter host port number:"))));
System.out.println("Waiting for client connection...");
try (final Socket sck = srv.accept();
final OutputStream os = sck.getOutputStream()) {
srv.close();
srv = null;
//These are the classes we need the client to load:
final Class[] clientClasses = new Class[] {
ClientMain.class,
MyStrings.class,
MyStrings.NestedStatic.class,
MyStrings.NestedNonStaticSubclass.class
};
System.out.println("Sending all client classes' bytes to client...");
final DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(clientClasses.length);
for (final Class clazz: clientClasses) {
final byte[] contents = Files.readAllBytes(getClassFilePath(clazz));
dos.writeUTF(clazz.getName());
dos.writeInt(contents.length);
dos.write(contents);
}
System.out.println("Main application now starts...");
//Here would go the server side code for the client-server application.
}
}
catch (final IOException | URISyntaxException | RuntimeException x) {
JOptionPane.showMessageDialog(null, x.toString());
}
finally {
System.out.println("Done.");
try { if (srv != null) srv.close(); } catch (final IOException iox) {}
}
}
}
ClientMain class:
package server;
import java.io.DataInputStream;
import java.net.Socket;
public class ClientMain {
//This method is called by the client to start the application:
public static void main(final Socket sck,
final DataInputStream dis,
final ClassLoader loader,
final String[] args) {
System.out.println("Running...");
//Here would go the client side code for the client-server application.
//Just test that all the classes are loaded successfully:
System.out.println(new MyStrings("A", "B", "C").new NestedNonStaticSubclass().getFunction().apply(2)); //Should print "C".
}
}
MyStrings class:
package server;
import java.util.function.IntFunction;
public class MyStrings {
public static class NestedStatic {
private final IntFunction<String> f;
public NestedStatic(final IntFunction<String> f) {
this.f = f;
}
public IntFunction<String> getFunction() {
return f;
}
}
//This class produces the error when loaded:
public class NestedNonStaticSubclass extends NestedStatic {
public NestedNonStaticSubclass() {
super(i -> getString(i)); //Here we refer to MyStrings object before constructing the NestedNonStaticSubclass object.
}
}
private final String[] array;
public MyStrings(final String... array) {
this.array = array.clone();
}
public String getString(final int i) {
return array[i];
}
}
Client side code:
package client;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Objects;
import javax.swing.JOptionPane;
public class LiveClientTest {
public static class MemoryClassLoader extends ClassLoader {
//The key is the name of the class, and value is the compiled class file bytes:
private final HashMap<String, byte[]> classByteCode = new HashMap<>();
#Override
public /*synchronized*/ Class findClass(final String name) throws ClassNotFoundException {
try {
return super.findClass(name);
}
catch (final ClassNotFoundException cnfx) {
if (!classByteCode.containsKey(name))
throw new ClassNotFoundException(name);
final byte[] byteCode = classByteCode.get(name);
return defineClass(name, byteCode, 0, byteCode.length);
}
}
//Try to load all classes that are downloaded (with readClass):
ArrayList<String> loadClasses() {
final ArrayList<String> classNames = new ArrayList<>(classByteCode.keySet());
int oldSize;
do {
oldSize = classNames.size();
final Iterator<String> classNamesIter = classNames.iterator();
while (classNamesIter.hasNext()) {
final String className = classNamesIter.next();
try {
loadClass(className);
classNamesIter.remove();
}
catch (final ClassNotFoundException x) {
}
}
}
while (classNames.size() < oldSize); //If we reached a point where there are some classes that can not be loaded, then quit...
return classNames; //Return the classes that failed to be loaded (if any) (should be empty).
}
//Read class bytes from supplied DataInputStream:
void readClass(final DataInputStream dis) throws IOException {
final String name = dis.readUTF();
final byte[] contents = new byte[dis.readInt()];
int i = 0, n = dis.read(contents);
//Make sure all 'contents.length' multitude of bytes are read:
while (n >= 0 && (i + n) < contents.length) {
i += n;
n = dis.read(contents, i, contents.length - i);
}
if (/*n < 0 ||*/ (i + n) != contents.length)
throw new IOException("Unfinished class input (" + name + " - " + contents.length + ").");
classByteCode.put(name, contents);
}
}
public static void main(final String[] args) {
try {
final String host = Objects.requireNonNull(JOptionPane.showInputDialog(null, "Enter host name or address:"));
final int port = Integer.parseInt(Objects.requireNonNull(JOptionPane.showInputDialog(null, "Enter host port number:")));
try (final Socket sck = new Socket(host, port);
final InputStream is = sck.getInputStream()) {
final DataInputStream dis = new DataInputStream(is);
final MemoryClassLoader loader = new MemoryClassLoader();
//Download all classes and put them into the class loader:
System.out.println("Downloading...");
for (int i = dis.readInt(); i > 0; --i)
loader.readClass(dis);
//Load all downloaded classes from the class loader:
System.out.println("Loading...");
System.out.println("Failed to load: " + loader.loadClasses() + '.');
//Call main method in main downloaded class:
loader
.loadClass("server.ClientMain") //package is from server side.
.getDeclaredMethod("main", Socket.class, DataInputStream.class, ClassLoader.class, String[].class)
.invoke(null, sck, dis, loader, args);
}
}
catch (final IOException | ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | RuntimeException x) {
x.printStackTrace();
JOptionPane.showMessageDialog(null, x);
}
}
}
Client side output:
Downloading...
Loading...
Exception in thread "main" java.lang.ClassFormatError: Illegal field name "server.MyStrings$this" in class server/MyStrings$NestedNonStaticSubclass
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.lang.ClassLoader.defineClass(ClassLoader.java:642)
at client.LiveClientTest$MemoryClassLoader.findClass(LiveClientTest.java:30)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at client.LiveClientTest$MemoryClassLoader.loadClasses(LiveClientTest.java:44)
at client.LiveClientTest.main(LiveClientTest.java:89)
So my question is:
Why does the code fail with a ClassFormatError, what does that mean, and how to avoid it in this particular scenario?
My question is not:
What alternatives exist? (such as using a URLClassLoader, or alternative ways of class loading from memory other than a custom ClassLoader, etc...)
How to reproduce:
I am using JRE 1.8.0_251 (and I would like this to work for 1.8), so I think you must put the source files in different projects (one for the client and one for the server) in order to make sure that the client does not already have direct visibility of the server's classes while class-loading them.
Run the server's main class server.LiveServerTest and input a port number for the server in the dialog that pops up. Then, run the client's main class client.LiveClientTest and enter localhost for the host (first dialog that pops up) and then the port number of the server (second dialog that pops up).
The stack trace will be in your CLI (through System.err) and not in a GUI.
The code will not work if you build the projects into jar files, but it is written to work for plain class files into directories, for simplicity. For example, in NetBeans, don't build into jar files, but rather click Run File for each entry point.
The built classes should have their file's .class extention in lower case.
Warning:
If you put the source files in different packages but in the same project, or even worse in the same package, then the class loading might succeed without errors because:
I am using the default constructor of the ClassLoader class in my LiveClientTest.MemoryClassLoader class, which means the parent class loader is the system class loader.
The LiveClientTest.MemoryClassLoader.findClass method first searches the parent ClassLoader and then, if that fails, it searches the downloaded classes. To my knowledge, this is the suggested way of implementing this, mainly because the ClassLoader class (which is the parent class of my LiveClientTest.MemoryClassLoader class) caches already defined classes.
References:
How to load JAR files dynamically at Runtime?
In which scenarios the remote class loading are needed?
Does the Java ClassLoader load inner classes?
Java - Get a list of all Classes loaded in the JVM
Java: How to load a class (and its inner classes) that is already on the class path?
Create new ClassLoader to reload Class
How to use classloader to load class file from server to client
Custom Java classloader and inner classes
ClassFormatError in java 8?
JVM Invalid Nested Class Name?
https://bugs.openjdk.java.net/browse/JDK-8145051
https://www.programming-books.io/essential/java/implementing-a-custom-classloader-0f0fe95cf7224c668e631a671eef3b94
https://www.baeldung.com/java-classloaders
https://www.infoworld.com/article/2077260/learn-java-the-basics-of-java-class-loaders.html
https://www.oracle.com/technical-resources/articles/javase/classloaders.html
https://www.ibm.com/developerworks/library/j-onejar/index.html
I am new to class loading, so please don't take my words for granted.
Huge post, because of the divided code of the MRE. Sorry. I tried to make the code as minimal as possible.

Generating a new java class by writing java program in another java class [duplicate]

This question already has answers here:
How do I create a file and write to it?
(35 answers)
Closed 3 years ago.
I want to create a java program that generates another java class in the same project. For example in the class Dragon.java, i want to write java code that creates another java class called fire.java. I do not want to use any GUI from eclipse, just pure code that generates another class from the execution of written programming in java.
I have tried making objects of a non existent class in hopes of the program automatically producing a class with that name.
Again, it doesn't have to be just a java class, is there a way to make other forms of files also? for example fol.flow, or of different names.
Creating a new Java file is easy. You can use any FileWriter technique. But what need to be taken care of is that new Java file is valid java file and can be compiled to class file.
This link has working example of doing the same.
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
public class MakeTodayClass {
Date today = new Date();
String todayMillis = Long.toString(today.getTime());
String todayClass = "z_" + todayMillis;
String todaySource = todayClass + ".java";
public static void main (String args[]){
MakeTodayClass mtc = new MakeTodayClass();
mtc.createIt();
if (mtc.compileIt()) {
System.out.println("Running " + mtc.todayClass + ":\n\n");
mtc.runIt();
}
else
System.out.println(mtc.todaySource + " is bad.");
}
public void createIt() {
try {
FileWriter aWriter = new FileWriter(todaySource, true);
aWriter.write("public class "+ todayClass + "{");
aWriter.write(" public void doit() {");
aWriter.write(" System.out.println(\""+todayMillis+"\");");
aWriter.write(" }}\n");
aWriter.flush();
aWriter.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public boolean compileIt() {
String [] source = { new String(todaySource)};
ByteArrayOutputStream baos= new ByteArrayOutputStream();
new sun.tools.javac.Main(baos,source[0]).compile(source);
// if using JDK >= 1.3 then use
// public static int com.sun.tools.javac.Main.compile(source);
return (baos.toString().indexOf("error")==-1);
}
public void runIt() {
try {
Class params[] = {};
Object paramsObj[] = {};
Class thisClass = Class.forName(todayClass);
Object iClass = thisClass.newInstance();
Method thisMethod = thisClass.getDeclaredMethod("doit", params);
thisMethod.invoke(iClass, paramsObj);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
At first I thought you wanted code generation, but you simply want to write to files or create them?
The simplest code to create file and write to it:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class Testing {
public static void main(String[] args) throws IOException {
Files.writeString(Paths.get("D://output.txt"), "some text to write", StandardOpenOption.CREATE);
}
}
It uses only java standard classes, you don't need any libraries or anything external. Just make sure to write to the valid path, where you have access.
If you want to generate files with java code, you can just do it with the method above, but creating the String with code content is really hard, there are libraries for it and they are not easy to use for beginners. For example javapoet. I personally used javaparser, it has a lot of other possibilities besides generating code.

Receiving java class object in Python

I've Java .jar file which has several classes defined and there is python framework which intends to pick any class from it, instantiate it's object and invoke it's method. To do this I'm using py4j JavaGateway().
At python side:
from py4j.java_gateway import JavaGateway
gateway = JavaGateway()
obj_rcvd = gateway.entry_point.getObj("pkg.in.jar", "className", java_list)
boo = pkg.in.jar.className(obj_rcvd)
"""
this typecast fails as python env doesn't know about pkg from jar. Can we import java's jar file in Python? Also if we can, do we really need to call JVM to get objects?
"""
At Java side:
import py4j.GatewayServer;
import java.lang.reflect.*;
import java.util.*;
public class EntryPoint {
public static Object getObj(String pkgName, String className, List args) {
Object obj = null;
try {
Class cls2 = Class.forName(pkgName + '.' + className);
int num_of_args = args.size();
Class[] cArg = new Class[num_of_args];
Object[] cArg_val = new Object[num_of_args];
/* code to parse args to fill cArg and cArg_val */
Constructor ctor = cls2.getDeclaredConstructor(cArg);
obj = ctor.newInstance(cArg_val);
}
catch (ClassNotFoundException x) {
x.printStackTrace();
}
/* other exception catchers */
return obj; // this is general Object type, hence I need to typecast in python
}
public static void main(String[] args) {
GatewayServer gatewayServer = new GatewayServer(new EntryPoint());
gatewayServer.start();
System.out.println("Gateway Server Started");
}
I tried returning actual class object from Java(hard-coded for one case for debugging) but it too doesn't get recognized in Python. Please suggest if this approach to invoke methods of java jar in python is feasible.

How can I print the AST from ASTEmbeddedAutomaton to a text file using the generated PrettyPrinter from Monticore?

I already made MontiCore generate the PrettyPrinter classes with the options explained in Configure pom.xml to generate PrettyPrinters for MontiCore Languages.
How can I generate the formatted text output of my MontiArcAutomaton (Type: ASTEmbeddedAutomaton) ?
After asking the MontiCore team, the following solution turned out be a good approach:
I wrote a helper class for pretty printing the embedded automaton ASTNode (see Listing 1).
This helper class can be used e.g. with the following code line:
System.out.println(PrettyPrintFactory.getInstance().printNode(autMoore));
Listing 1:
import mc.ast.ASTCNode;
import mc.ast.PrettyPrinter;
import mc.helper.IndentPrinter;
import mc.maautomaton._prettyprint.MontiArcAutomatonConcretePrettyPrinter;
/**
* Prints the AST-Content to a file
**/
public class PrettyPrintFactory {
private static PrettyPrintFactory instance;
private PrettyPrinter pretty;
public static PrettyPrintFactory getInstance() {
if (instance == null) {
instance = new PrettyPrintFactory();
}
return instance;
}
private PrettyPrintFactory() {
pretty = new PrettyPrinter();
pretty.addConcretePrettyPrinter(new MontiArcAutomatonConcretePrettyPrinter());
}
/** prints the syntax of the astNode to a String */
public String printNode(ASTCNode astNode) {
IndentPrinter ip = new IndentPrinter();
pretty.prettyPrint(astNode, ip);
return ip.getContent();
}
}

Categories