I have a Java Application that uses JPA on a file DB.
I want only one instance of my application running (Note that the DB gets locked to the first instance of the application).
How could I check if my DB is locked or not, and present a message to the user?
Telnet your DB with its ip and the port number.
Eclipse solve this solution with a .lock file. Other solution can be: try to use a port: bind to port 12345. If you can than you are the only one king of the hill if you can't, than maybe other are using that hill. Any other apps can.
The .lock file has many disadvantages too.
And the last and best solution is: do it with platform dependent at way at OS level via JNI, but require a lot of work and has disadvantages too.
Related
Production Environment: Tomcat 9 on CentOS 7 x64, mysql/mariadb 5.5x
Testing Environment: Tomcat 9 in Eclipse on Windows 7 x64, mysql 5.5x
I'm a Tomcat newbie looking for the best method to have server-wide variables readable/writable from all Webapps for things like MaintenanceMode(on/off) and MaintenanceMessage, etc.
These are the variable properties I'm looking for:
Readable/writable from all instances of all java servlets on all webapps.
Value persists after OS, Tomcat, or Webapp restart.
I must be able to change the value of it from one webapp and then all other webapps recognize the change quickly, ideally without restarting.
Ideally I wouldn't want to read the variable from disk on each server request. In case server is getting DDOSed or something.
Ideally the solution is OS independent.
If it's a disk file solution please recommend a place for me to store the file.
I'm new to Tomcat so some detail in any answers would be appreciated or links to detail. I'll probably be using a servlet on it's own 'admin' webapp that's only accessible through SSH-tunneling, etc, to set the variables. Then the public webapps would respond to any changes, like showing a maintenance message while I backup databases. I could also possibly change the variables using linux commands if needed.
If I stored the server variables in a database that could be fine but I wouldn't want to read the DB on every single request most likely, and when I change a variable I'd have to once again notify every webapp/servlet that something was changed and to re-read the DB.
Thanks for any help.
I implemented this recently in the form of "system messages", some of which are for maintenance. But the effect is the same. We had some additional "requirements" which helped us form the solution. These may or may not match up to your expectations/desires:
Multiple-server coordination
Immediate synchronization was not necessary
We used our relational database for the actual data storage. A single table with "system messages" and a few other fields such as when the messages became effective (not_before DATETIME) and when the messages became ineffective (not_after DATETIME).
On startup, the application reads the system messages table to find the currently-valid messages and stores them in application scope, including their validity dates. Whenever we want to show them on the screen, we check each item in memory and evict any that have expired. This is pretty fast.
Every X minutes (e.g. from cron), we make a request to a special servlet (on each server) that re-loads the system messages from the database. We protect that servlet by only allowing requests from certain IPs, so DOS is not an issue.
Not only can we add a system message from any server in the cluster, but we can also add one by writing directly to the database. That may be advantageous if you don't always want to use your application to generate these events.
You can change the value for X to anything as low as 1 (minute) if you are using cron. If you use some other kind of system, you can probably get updated even more often. I would seriously reconsider your requirement of "immediate" recognition because that requires a system that has much worse performance.
If you can guarantee that only your application can generate these changes, and you have a list of all other servers in the cluster somewhere, you could theoretically ping them all with the new message (or notify them that a new message exists and it's time to update their message-list), but that kind of thing is better-done with an orchestration tool such as Kubernetes, and we are getting a little out of scope IMO.
I have an entry in my .ssh/config file that looks like:
Host ip-172-16-*
ProxyCommand ssh jeff#jumpdev.example.org nc %h %p
User ubuntu
IdentityFile ~/.ssh/id_rsa_aws
StrictHostKeyChecking no
ForwardAgent yes
As most know this will allow me to run:
ssh ip-172-16-2-108.ec2.internal and I drop onto the private server, as the default user for Ubuntu EC2 instances - ubuntu. I'm looking to take some specific actions on create.
Ultimately what i am looking for is the same functionality with either scala-ssh, jassh or directly from jsch
I'm not asking for anyone to suggest a library, just how/if this functionality is supported. With jassh, i've attempted with the Proxy options with no luck.
Ultimately i could shell out directly to ssh, but even with ammonite, it won't load the config if using %('ssh, Symbol("ip-172-16-2-108.ec2.internal"). I thought that would use the base ssh, which would load the config and match, like bare ssh. I could also set up my config not to use nc
This does sound like i'm asking for a library, so if it gets closed, so be it, but i've tried 4 directly and none, seem to completely support .ssh/config
TIA,
Jeff
I know there are other answers, but I found that by switching from ProxyCommand to ProxyJump in my .ssh/config allowed ammonite to work perfectly. Also since all I need is a solution to this problem and am already using ammonite, it is a good match.
If others have other experiences, I think this is a good question to leave open, as we're talking about specific libraries which are all built off Jcsh.
FWIW, my .ssh/config now looks like:
Host ip-172-16-*
ProxyJump jeff#jumpdev.example.org
User ubuntu
IdentityFile ~/.ssh/id_rsa_aws
StrictHostKeyChecking no
ForwardAgent yes
Hope this helps someone.
Is it possible that a java program is running and it works on commands from another java program simultaneously running in same machine? For example: if a second java program sends a query database command to first running java program, the first one will execute a query in the database connected to it and reply back to second one.
Please help.
Thanks in advance.
You could use socket programming to do this.
Make a server and make it listen to incoming messages.
http://docs.oracle.com/javase/tutorial/networking/sockets/
You could learn about sockets from the above link.
It is possible.
Socket programming is good but in this case you have to implement synchronization and multi threading request handling.
Another way is using web service for storing data in db.
http://docs.oracle.com/javaee/6/tutorial/doc/gijvh.html
Probably you can use any of
remote invocations (RMI), see http://docs.oracle.com/javase/tutorial/rmi/
or
manged beans (JMX), see http://docs.oracle.com/javase/tutorial/jmx/
As already answered you can use socket proramming but you would have to implement your own protocol.
It should be easier to use RMI which lets you invoke remote methods as if they were local but it is limited to java.
A "bit" heavier (in resources and implementation) solution is using web services but it is a standard which is not limited to java world.
You can also use JMS but I think it should be overkill (you need a server such as activeMQ)
Is it possible to invoke an exe on a remote computer, supposing we know the absolute path of the exe where it lies?
I have written a client and a server program and am able to get both work as expected when both are running. But now, all I want to do is to invoke the server program from client if server is not running. Seems like defeating the purpose of server-client model but still.
If that is possible without knowing the login credentials of the remote system, then it'd be cool. Eg: There are softwares like 'PsExec' that need login credentials.
I understand that, such a feature may not be there, as it would mean a serious security threat, but, I am just wondering if there is some sort of a workaround.
If that is possible without knowing
the login credentials of the remote
system, then it'd be cool. Eg: There
are softwares like 'PsExec' that need
login credentials.
I understand that, such a feature may
not be there, as it would mean a
serious security threat, but, I am
just wondering if there is some sort
of a workaround.
Those "inconvenient" security rules are there for a purpose. You simply do not want people to be able to run any old program on your machine without proper authentication and authorization. Not even if you are behind a firewall.
Why?
In one word - Viruses! If some machine on your network gets infected with a virus (or similar malware), then all machines that run a non-authenticated remote execute service would be at serious risk of infection.
Try Gridserver technologies... it's based on grid technology and very efficient and easily deployable.
You could setup a messaging system like a remote procedure call, webservice, or simple tcp socket on the server to do a wake up on the server process.
The only way you can execute an exe is by executing a local application which you have access to. There must be a part of the software running on the computer you want to execute an application on. From there, you can do a remote call from a second program on a remote machine asking the local one to do his job : execute.
As stephen C said, it would be a violent breach of security if I could run exe on your machine without your permision.
I'm currently writing a Java application to be used with a Windows-Machine authed with an ActiveDirectory. The application basically only needs to know the user's name and hostname. I know there are
System.getProperty("user.name")
and
java.net.InetAddress.getLocalHost().getHostName()
But I am not sure wether System.getProperty("user.name") will function correctly with the VM running on windows (I searched google and found a lot of threads saying it might not work with windows, as it might return something different, depending on the environment-variables
(and I am currently unable to test it [I'm running ubuntu and archLinux]).
So, I wondered if there is a better and more secure way to handle this and stumbled upon NTSystem .
But NTSystem does not seem to be available on Linux (which I use for developing), which - I think - is due to calling native windows code.
My question would hence be: "Is there a secure way to retrieve the logged in user's name in Windows and if yes - how would you accomplish that?"
user.name is inherently insecure because it can be overridden via -Duser.name=XYZ. This might be an issue for you, or it may not be
Obviously NTSystem won't work on Linux but you mention that you are writing a GUI to be run on Windows. Are you trying to validate the Windows user name of the user? You can do this via NTSystem embedded in the code which runs on the Windows client but not (of course) code which runs under the Linux OS.
Or are you trying to validate them on a Linux server? Perhaps you have a kerberos domain you could do this with? (i.e. if there is a kerberos domain, then you can have a secure, authenticated communication between client and server, ensuring that the client is who they say they are)
EDIT: I may be confused by the fact you are saying that you're writing a Java App "in Linux". I took this to mean a Linux server and Windows client - but possibly you just mean that you are using Lenux as your development environment? In this case, you might think of writing a pluggable identification layer which you can switch between using NTSystem on the Windows box and user.name for testing
Use JNA, com.sun.platform.jna.win32 has a number of methods to do this wrapping the Win32 API. Try Advapi32Util.getUserName or Kernel32Util.getUserNameEx.