Deploying an executable JAR on Azure functions without docker image? - java

I am trying to replicate on Azure Functions the example that is in Quickstart: Create a Go or Rust function in Java. I set up a couple of methods that respond to REST requests, and I bundled them as well as a Jetty server, that manages and routes the requests, into an executable Uber JAR. When I run java -jar handler.jar, an instance of a Jetty server is started, so I can navigate to http://localhost:8080/ping. I can achieve the same by using the JRE I have installed on my computer when using func start with the following host.json:
"customHandler": {
"description": {
"defaultExecutablePath": "java",
"arguments": ["-jar", "handler.jar"]
},
"enableForwardingHttpRequest": true
}
Now, I'm trying to deploy this app to Azure Functions. One possible way would be to create a custom image with the following Dockerfile:
FROM mcr.microsoft.com/azure-functions/java:4-java11-slim
ENV AzureWebJobsScriptRoot=/home/site/wwwroot
COPY ["./functions", "/home/site/wwwroot"]
and start a function by using this image - I tried this and it works ok, but it seems to be quite inefficient to create a 1GB+ image, when the JAR I want to deploy takes up just a couple of MBs.
I was wondering, if there is a way to avoid this step? For example, push only the JAR and all the .json files to functions and use the default JRE, that is included when I create a Function App with the Java runtime stack? In other words, is it possible to achieve what I achieve locally when using func start, but use the JRE that is on azure instead?
The documentation ("If your handler requires operating system or platform dependencies (such as a language runtime), you may need to use a custom container.") is not that very clear about the topic.
Thank you!

Have a look at the Triggers and annotations paragraph of the Azure Functions Java developer guide.
In the function.json, set the scriptFile field to the (relative) location of your jar, and set entryPoint to the location of the method the Azure Function should execute. Then using the Azure Functions Core Tools, you can do func azure functionapp publish {function_app_name_in_Azure} --java to publish too the Function App in the cloud.

Related

How to convert AWS Lambda to create an image for AWS Batch

Currently have java lambda code packaged as .zip files and deployed to AWS Lambda, but needs to move to AWS Batch due to long running tasks that exceed the 15 minute lambda quota
It is possible to deploy the java lambda code using container images with the docker CMD set to the handler in the Dockerfile (e.g. https://hub.docker.com/r/amazon/aws-lambda-java).
FROM public.ecr.aws/lambda/java:11
# Copy function code and runtime dependencies from Gradle layout
COPY build/classes/java/main ${LAMBDA_TASK_ROOT}
COPY build/dependency/* ${LAMBDA_TASK_ROOT}/lib/
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "com.example.LambdaHandler::handleRequest" ]
So the code can still be deployed to AWS Lambda as a container image. Problem is it's still a lambda, so using that image for AWS Batch makes no sense right...
Gurus🙏 Given that the lambda logic is tried and tested:
What is the simplest way to convert the lambda code with the handleRequest method to work with AWS Batch -> perhaps execute from a JAR and to prepare an image so that in can work with AWS Batch?
There would need to be a main method for an entry point instead of the handler?
Lambda container images are for custom environments that need to run on AWS Lambda. Functions built as container images should still follow Lambda’s invocation model, so the functions will still have a 15 minute max duration. It's not a replacement for ECS.
To use your function with Batch, you will have to build a docker image with the function's code and all dependencies, push it to ECR, use it with ECS, and use Batch API's SubmitJob for invocation with environment variables if necessary.
Yes, you will need an Entrypoint/other CMD instead of the LambdaHandler depending on your logic. Just anything that runs the function and exits the container with CODE 0
I tried this for a python lambda and it seems to be working fine:
Take the standard lambda base image (e.g public.ecr.aws/lambda/python:3.8). It has a predefined argument ["entrypoint.sh"] set as the docker image ENTRYPOINT, so when using it for a "non-lambda" purpose you'll need to override that (set it to []) and pass the Command you want.
I did the overriding of ENTRYPOINT in my Dockerfile so that it can be shared with AWS Batch tasks. When called from lambda infrastructure I explicitly set ENTRYPOINT back to ["entrypoint.sh"].
Overriding Entrypoint does feel a wee bit hacky.

How to deploy a Java Play App in production using a Windows environment?

I have built a Java Play app using the latest version. Currently I am running the application using the batch file that gets generated. There's a zip file created inside target->universal folder (When I go to the bin folder inside the extracted folder this batch file is available).
How can I run this in production Windows environment? This play app is a backend service that needs to run continuously. Should this be created as a Windows Service? As in, should I try to run this batch file as a Windows Service through some means? I can't find any online references on this. Any help would be much appreciated.
There are service wrappers available, that can take almost any arbitrary application and run it as a service or daemon on some operating systems. I would highly recommend that you avoid them. Writing secure, performant services for Windows is not trivial. You can however run any script using Windows TaskManager (GUI) or Task Scheduler, and it has all the knobs you need to easily control how your application runs, when it starts and what to do if it crashes.
See schtasks.exe for command line interface reference.

Message Passing from NetBeans Plugin to C# WinForms Application

The short version of this question is, how can I send a message (a file path, for example) from a Java application (NetBeans plugin, to be specific) to an already-running C# WinForms executable application?
Some details.
I currently have a Windows application that I built (it's like a parser/editor for script files to help automate the process of script building using simple GUI tools for our test team). In the application, you can load a file using the app's "Load" button, hotkey, drag/drop, etc. You can also open the application from the command line with one or more file paths as parameters, and the app will launch with the given files already loaded.
On request, I cobbled together a NetBeans plugin (Java) that grabs the file path of the file that is currently in focus in NetBeans, and then executes my application with the focused file's path as parameter. This allows me to launch my app directly from NetBeans and open the file being edited in NetBeans in my app.
So what my app can do:
Launch with file loaded from CLI parameter
Load file from internal load command on-the-fly
What it can't do (what I want to add):
Load file in running instance from external message parameter on-the-fly
Load file in running instance from CLI message parameter on-the-fly
MSMQ is a solution I can't use. Saw it suggested in a lot of other threads. Things to note are that the NetBeans plugin and my app will always be on the same system, but I can't get MSMQ on all target machines.
Two suggestions:
(1) If you can figure out a way to publish an event from a Java process into the windows event log, you can definitely set up your .net app to watch for specific event types
(2) If you can arrange for the .net app to watch for files being created in a particular directory using FileSystemWatcher (maybe in %TEMP%), you can have your Java process write a file that contains whatever info you wish to pass.

Simple and flexible method to run java as a windows service

I read SO q&a about launch java as windows service wrappers, but can't find/choose product, suitable for my requirements :
Wrapper reads all java launch parameters from config file. In registry must be only commands to run wrapper itself - path to exe and maybe path to config. Always grant admin rights to change app parameters in registry is not comfortable.
Working dir and path to application also must be in config. I want to place all wrapper files deep into program, because user not intrested in it.
No dependencies(.net, python, etc). I don't want to extend requirements of my program.
Free for every windows OS, include win64.
Simple and lightweight. Size of my program with all 3rd party jars is 12Mbytes. I don't want to add 20Mbytes wrapper.
Automaticaly restart if app process dies.
Preferably single exe files for all windows platform.
I try and lookinkg throught different wrappers and tired a bit. Best suitable till now is:
jslwin (http://jslwin.sourceforge.net), but i don't know how mature it is(version 0.99). Also parameter lists in config is a bit complex(need to edit parameters quantity).
winrun4j (http://winrun4j.sourceforge.net), but i don't know how mature it is(version 0.45). Also it needs a special class for working as a service.
Also i try or look throught:
YAJWS (http://yajsw.sourceforge.net/) too big (30mb), can't make it work with custom files layout.
Advanced Installer (http://www.advancedinstaller.com) too big (50mb).
nssm (http://nssm.cc/) store launch parameters in registry(no config)
winsw (https://kenai.com/projects/winsw) .net required, stops after process dies
commons-daemon, procrun (http://commons.apache.org/proper/commons-daemon/index.html) store launch parameters in registry(no config), can't understand this product, i think it's complex.
jsmooth (http://jsmooth.sourceforge.net) complex and old(last update 2007)
javaservice (http://forge.ow2.org/projects/javaservice/) old (last update 2006)
runasservice (http://runasservice.sourceforge.net/) requires .net
windows rktools - sc, instsrv, srvany (http://support.microsoft.com/?scid=kb%3Ben-us%3B137890&x=16&y=7) - store launch parameters in registry(no config)
jwrapper (http://www.jwrapper.com/) seems not what i need. It packs java program in exe file with JRE. It's not about windows services.
firedaemon (http://www.firedaemon.com/) not free. And maybe it's not what i need.
launch4j (http://launch4j.sourceforge.net/) cool ability "search or download java". Maybe i add something like this to simplify installation, but it is still not about subj.
Java service wrapper (http://wrapper.tanukisoftware.com/) not free for win64.
Tell me please which wrappers suits my needs? Or maybe i think wrong about wrappers above and some of them suits my needs to?
I have had similar issues over the years. Its not that they all are inferior, its that they are all pretty hard to integrate nicely with your own project.
You may want to check out stork: https://github.com/fizzed/stork
It'll help create launchers for your Java console or daemon apps, and they will work across all major platforms. There is also a maven plugin in case you wanted to tie it into your existing build process.
I'm not sure if JWrapper will fulfil all the requirements like 1 and 2 in the way that you want it since rather than you dictating how it does the launch and where it puts things etc JWrapper instead handles that stuff in its own way but it does produce executables for win/mac/linux and more importantly it does support windows service installation as well as macos and linux daemon installation:
http://www.jwrapper.com/guide-installing-a-service.html
The service support though is for paid licenses, the free version doesn't currently support it.
Disclosure: I work for JWrapper

Java application launcher

I have written a very simple Java application. Can anyone tell me how to create a launcher like icon to run that application both in Ubuntu and Windows ??
Thanks in advance..!!
An executable JAR should work fine for a launcher on both Windows and Linux. However, that won't get you a custom icon.
On Windows, you can use JSmooth, which will create a .exe wrapper around your JAR file. The JSmooth program will let you create an icon for the EXE as well (it also has options such as getting the user to download the necessary version of the JVM, or only permitting one instance of the program to run).
If your java application is to be distributed from a web server, you should have a look at Java Web Start which can do what you ask for based on a JNLP-file. Notably see
http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/guide/javaws/developersguide/faq.html#104
Ubuntu and Windows will both have different ways to launch the application. I see two routes to follow here. One is to use Java Web Start and use a web interface to start your application. The other is to search for some sort of 3rd party installer that will create setup/installation programs for all the platforms you want to use.
I actually used a third party install program before, InstallAnywhere, but it was almost ten years ago. It offered the functionality you are looking for, though.
There are different ways to do this, sometimes the simplest is the best approach. One solution as suggested is a simple starting script. Roll your application into a jar, then include a script that does nothing more than "java -jar myscript.jar". I do this all the time for internal customers that may be running various types of *nix and whatever version of windows (a few macs as well). How sophisticated you need it to be depends on the audience served.
Create a bat/shell script which starts your application.
And than create a shortcut/launcher for it.
Shortcut file in windows has extension lnk.
Update
See example - SQuirreL launch file.

Categories