Exporting data from dev_appserver - java

I'm testing a schema change over two versions of my app. I used version 1 to generate test data, and now I'd like to take that data into version 2 to run and test the converter. This is easy enough to do live on appengine, since the datastore stays persistent between versions, but I'm finding that local_db.bin does not survive from one version to the next (maybe this is because the version of the sdk also changes between versions).
I'd like to use appcfg.py to download_data from dev_appserver and then upload_data to the new version, but it seems to be asking me to download each kind of entity individually ("Kind stats are not available on dev_appserver.").
I can write a script that iterates through all of my kinds to use download_ and upload_data. Is there an easier way to transfer data between instances of the dev server?

One unelegant solution:
bash script to pump data out:
KINDS="Assessment AssessmentScore Course GradingPeriod GradingPolicy OverallGradeDefinition Standard StandardTag User"
for KIND in $KINDS
do
echo "ugh" | appcfg.py download_data --filename=$KIND --kind=$KIND -email=blagh --url=http://localhost:8888/remote_api --passin --application=myapp
sleep 5
done
And a corresponding script with upload_data to pump it back in. Getting pretty kludgy when you're using bash to drive python to drive http requests to your java app!

Related

Automated updates for Java applications [duplicate]

I would like to implement a java application (server application) that can download a new version (.jar file) from a given url, and then update itself at runtime.
What is the best way to do this and is it possible?
I guess that the application can download a new .jar file and start it. But how should I do the handover, e.g. know when the new application is started and then exit. Or is there a better way to do this?
The basic structure of a solution is as follows:
There is a main loop responsible for repeatedly loading the latest version of the app (if required) and launching it.
The application does its thing, but periodically checks the download URL. If it detects a new version it exits back to the launcher.
There are a number of ways you could implement this. For example:
The launcher could be a wrapper script or binary application that starts a new JVM to run the application from a JAR file that gets replaced.
The launcher could be a Java application that creates a classloader for the new JAR, loads an entrypoint class and calls some method on it. If you do it this way, you have to watch for classloader storage leaks, but that's not difficult. (You just need to make sure that no objects with classes loaded from the JAR are reachable after you relaunch.)
The advantages of the external wrapper approach are:
you only need one JAR,
you can replace the entire Java app,
any secondary threads created by the app, etc will go away without special shutdown logic, and
you can also deal with recovery from application crashes, etc.
The second approach requires two JARs, but has the following advantages:
the solution is pure Java and portable,
the changeover will be quicker, and
you can more easily retain state across the restart (modulo leakage issues).
The "best" way depends on your specific requirements.
It should also be noted that:
There are security risks with auto-updating. In general, if the server that provides the updates is compromised, or if the mechanisms for providing the updates are susceptible to attack, then auto-updating can lead to a compromise of the client(s).
Pushing a update to a client that cause damage to the client could have legal risks, and risks to your business' reputation.
If you can find a way to avoid reinventing the wheel, that would be good. See the other answers for suggestions.
I am currently developing a JAVA Linux Daemon and also had the need to implement an auto-update mechanism. I wanted to limit my application to one jar file, and came up with a simple solution:
Pack the updater application in the update itself.
Application: When the application detects a newer version it does the following:
Download update (Zipfile)
Extract Application and ApplicationUpdater (all in the zipfile)
Run updater
ApplicationUpdater: When the updater runs it does the following:
Stop the Application (in my case a daemon via init.d)
Copy the downloaded jar file to overwrite current Application
Start the Application
Cleanup.
Hope it helps someone.
I've recently created update4j which is fully compatible with Java 9's module system.
It will seamlessly start the new version without a restart.
This is a known problem and I recommend against reinventing a wheel - don't write your own hack, just use what other people have already done.
Two situations you need to consider:
App needs to be self-updatable and keep running even during update (server app, embedded apps). Go with OSGi: Bundles or Equinox p2.
App is a desktop app and has an installer. There are many installers with update option. Check installers list.
I've written a Java application that can load plugins at runtime and start using them immediately, inspired by a similar mechanism in jEdit. jEdit is open source so you have the option of looking to see how it works.
The solution uses a custom ClassLoader to load files from the jar. Once they're loaded you can invoke some method from the new jar that will act as its main method. Then the tricky part is making sure you get rid of all references to the old code so that it can be garbage collected. I'm not quite an expert on that part, I've made it work but it wasn't easy.
First way: use tomcat and it's deploy facilities.
Second way: to split application on two parts (functional and update) and let update part replace function part.
Third way: In your server appliction just download new version, then old version releases bound port, then old version runs new version (starts process), then old version sends a request on application port to the new version to delete old version, old version terminates and new version deletes old version. Like this:
This isn't necessarily the best way, but it might work for you.
You can write a bootstrap application (ala the World of Warcraft launcher, if you've played WoW). That bootstrap is responsible for checking for updates.
If an update is available, it will offer it to the user, handle the download, installation, etc.
If the application is up to date, it will allow the user to launch the application
Optionally, you can allow the user to launch the application, even if it isn't up to date
This way you don't have to worry about forcing an exit of your application.
If your application is web based, and if it is important that they have an up to date client, then you can also do version checks while the application runs. You can do them at intervals, while performing normal communication with the server (some or all calls), or both.
For a product I recently worked on, we did version checks upon launch (without a boot strapper app, but before the main window appeared), and during calls to the server. When the client was out of date, we relied on the user to quit manually, but forbid any action against the server.
Please note that I don't know if Java can invoke UI code before you bring up your main window. We were using C#/WPF.
If you build your application using Equinox plugins, you can use the P2 Provisioning System to get a ready-made solution to this problem. This will require the server to restart itself after an update.
I see a security problem when downloading a new jar (etc.), e.g., a man in the middle attack. You always have to sign your downloadable update.
On JAX2015, Adam Bien told about using JGit for updating the binaries.
Sadly I could not find any tutorials.
Source in German.
Adam Bien created the updater see here
I forked it here with some javaFX frontend. I am also working on an automatic signing.

Can WireMock play and record be used at the same time?

I have an application that connect to an external third-party API, and of late the test instance of the API has not been particularly reliable. This stops development progress and turns our build pipelines to red, so I'd like to add an HTTP recording proxy to stop this happening.
I've had WireMock recommended, and having tried its record and play features, it does seem to be very good - it pretty much worked out of the box. We simply change the configured URL for the external service, and then record like this:
java -jar wiremock-standalone-2.3.1.jar \
--port 8080 \
--proxy-all="https://test-api.example.com/" \
--record-mappings \
--verbose
This creates cache folders in the current working dir, and then it can be switched to playback mode by killing the recorder and switching to playback:
java -jar wiremock-standalone-2.3.1.jar \
--port 8080 \
--verbose
However, to use this in practice I would need to set up an instance of my app running through a test regression pack to record lots of API usage, and to kick that off say one a day. I'd then need to stop the recorder and copy the cache files over to a playback process and then restart the playback process.
This probably would work, but it feels like a lot of moving parts, and ideally I would like to run play and record at the same time. This would allow the cache to be automatically refreshed if a new API call becomes necessary (due to natural project changes) but would playback by default where a match is found.
Is this possible? I am not a Java programmer, but suppose it might be available if one were to write a WireMock plugin. It would be great if this could be done at the console, but the phrasing of the manual indicates that play and record are thought of by the maintainers as separate things.
I did wonder whether I might switch to Mountebank, which looked like it might support this, however it turns out that play and record are separate modes here too. In any case, I like how easy WireMock has been to get started, so would like to stick with it if possible.
WireMock won't quite do what you're asking at the moment, however:
1) You can use the --proxy-all parameter and not --record-mappings when playing back. This will cause any request unmatched by an existing stub mapping (recorded or otherwise) to be proxied through to the actual service.
2) A workaround that'd get you pretty close to what you're after would be to send a POST to /__admin/mappings/reset endpoint after you'd collected some new recorded mappings. This causes the filesystem to be scanned and all the mappings (re)loaded.

Export custom probes graphs from offline run in JProfiler

I'm using offline profiling in JProfiler (in a remote machine) and I was trying to export the graphs from the snapshot using the jpexport command. However, from some reason, I can't get the ones from my custom probes, even if when I use the JProfiler GUI to open the snapshot, I have access to those graphs.
Note that I'm collecting data through the Profiling API, using Java classes which implements the 'TelemetryProbe', for example. In the documentation, there is also the possiblity of using the Custom Probe Wizard, and I wonder if I have to do by this method to get what I want, after exporting the session files to the profiling machine.
For the jpexport, I tried to export the graphs from 'ProbeTelemetry', following the instructions for the custom probes, giving the ID 1 to get my first probe, with no success. I even tried 0, but still, it returned saying there was no probe with such ID.
Still, I repeat, when uploading the snapshot with the GUI, the graphs are there, so JProfiler is collecting the information, I just can't figure out how to access it.
Any suggestions? Thank you.
As probe names for the export, use
-probeid=1
where n is the 1-based index of the custom probe.

How to insert data in OpenTSDB (Time Series Database)

I'm new to OpenTSDB. I somehow managed to install and configure OpenTSDB, but I don't know how to put data into OpenTSDB from client. Can anyone of you help me?
Have you read the getting started guide
You have several options. Once started, and metrics created via ./tsdb mkmetric mymetric.data_1 mymetric.data_2 , you can batch import files written in the form
metric_name timestamp value tags e.g.
mymetric.data_1 1295643636 48 a=foo
then you can ./tsdb import file
An other way is to "telnet" commands to the server in the same form as above, but with the put keyword, like echo "put mymetric.data_1 1295643636 48 a=foo" | nc -w 15 tsdHost tsdPort (linux way :-) )
The short answer is that those "put name timestamp value [tags]" commands are the network protocol for adding data to OpenTSDB over TCP/IP. So from your Java program, you can open a TCP socket to the TSD port and start writing commands to add your data.
Obviously, that still leaves a lot of details an an excercise to the reader.
You didn't specify in your question how, or even if, you're collecting time series data that you want to store. How complicated it is to periodically push data into TSD from your application will depend on what you're doing there. If you're using the excellent Yammer/Coda Hale Metrics library to collect data (and in Java, there really isn't a slicker way to do it, that I know of)
Metrics includes a reporter interface with several implementations for other statistics repositories like Graphite and Ganglia. You can easily implement one for OpenTSDB (in fact, someone already has - I'm trying to find the pull request on github but failing; I'll update this when I find it).
Check out the OpenTSDB 2.0 documentation:
http://opentsdb.net/docs/build/html/
This will provide better insight in adding data into openTSDB. Moreover, openTSDB2.0 has HTTP api to put data.
You can read Input Methods in the documentation.
Also I find it useful for me to read Data Specification, to understand what every time series consists of.

Using a Java program I made on a Web Server

I created a small application that, when run, creates or updates some tables in a database by extracting data from some PDF files. Everything works fine in this desktop application, but the next step for me would be to make it possible for an administrator on a website to upload a PDF file and my Java program would then run and update the tables accordingly.
The problem is I have no idea where to start with this (the site isn't done yet, but I'm running some tests and it is going to be coded in PHP). I'd like to know what kind of technologies I need to let the server run the program and update everything as it would in the offline version. Sometimes it takes a while to update everything, so ideally, the user uploading the PDF could continue browsing other pages while the server does its job. (I'll probably implement something that when the server is done processing the file, it says if the program ended successfully or not in a log file)
Can someone tell me what terms to search for on Google or give me some pointers? I haven't chosen where my website is going to be hosted either, so if someone could tell me what to look for to know if they support running applications like this, I'd really appreciate it as well!
This could also apply to other programming languages as I know a bit of Python and C++ as well, so in the future I might have some applications in those languages I'll want to use on the web.
If I'm not approaching this the right way, I'm open to other suggestions, but the best solution would be to keep my Java program intact as I know it works exactly like I want it to and I'd rather not have to start it all over again.
If your host is *NIX based you can use crontab (Automatic Task Scheduler) to run your program at set intervals. Make it check if a "new" PDF exists, and run the program if there is. There may be a way to use Windows Task Scheduler type programs to do it on Windows. This is probably the easiest way.
Alternately you can use You can use shell_exec() in your php to execute a command on your *NIX system directly to run your java program.

Categories