confused what to use- Tomcat or java socket server - java

I want a piece of java code to run on a particular port.
for exampple localhost:007 will return {key: value}
should i write a socket server in java, or should I use Tomcat?

The beautiful thing about Java is that there are so many existing libraries and frameworks out there that there's almost always an existing tool or framework to solve your problem. It will save you a heap of time to use Tomcat or some other lightweight container like Jetty for this task.
Alternatively, if this is a learning exercise then writing your own simple web server sounds like good fun!

Related

Communication between java spring server and python applications

I designed my java spring application to run a couple of python programs on same server and communicate with them. I run them with ProcessBuilder and communicate via InputStream/OutputStream.
Now I want to achieve that when I restart or shut down my java application, python apps didnt close. I can't get Process object by PID. With ProcessHandler object I can't get input/output streams. It seems that i should use some other mechanism of IPC. So the questions are:
How can I run external applications from java so they wouldn't close when java app restarts?
How can I achieve communication between java and python applications without having Process object?
Thanks in advance, sorry for poor language :)
I give some suggestion on our project experience:
In single server, use docker-compose to run/stop app no matter using java or python. In multi-server,they will be deployed in Istio.
use Restful protocol to communicate between them.

Java: how to write a wsgi server

I'm writing a web server by Java. This server is just to use socket to get the connection from client, parse the request and call the corresponding handler to handle this request. But I want to make a page using Django. However, I don't know how to run python script when the server get the request, and Django site tell me that it can be used in Apache by wsgi, so I guess I can do it as well, but how to implement it?
To get a good idea of what is WSGI you can read the official documentation (PEP-333). But basically WSGI is a big hash table that you pass to your WSGI application.
Since you are writing your server with Java an important part of your project will be to figure out how to call python code from inside your Java code.
But if you avoid writing this Java web server, you can tale a look at other tools that will give give your a better support to run your python WSGI app.
Apache with mod_wsgi
Mongrel2 with wsgid
Nginx with gunicorn
Good luck!
It is a bit confusing as to what you are really after. If you want to run Python WSGI applications, including Django on a Java server, then look at Jython and modjy.
http://opensource.xhaus.com/projects/show/modjy

Sending commands to a java program from a website

I was wondering if it's possible to send commands from a website to a java program being ran on a computer.
Basically what I'm doing is creating a robot, but I want control over it when I'm away from my computer. So what I was thinking, was that if I could send it commands (Like 'Stop' or 'Start') from a website, I could use my smartphone to control it.
If you know a way that might work or another method that's similar please let me know, thanks!
What I've done in the past is built the Java robot into a Java EE webapp, then deployed the webapp on Tomcat. Tomcat is a Java-based web server. It's a web server, but there's also no reason you can't run arbitrary code inside it, like a robot.
Another alternative is to embed a web server into the robot, and have the robot serve up pages itself. An example of an embedded HTTP server is JETTY.
Using the above two approaches, the web pages and Robot can communicate with each other directly through Java code. It's a single process and a single JVM running both.
A third alternative is to connect the Java robot process with the web server process via sockets or another form of IPC. This could be tricky, but decouples nicely.
one starting point is:
RPC the wikipedia article will guide you through other options and names that can help you. If it is really that basic. Socket Programming is the right way.
I was wondering if it's possible to send commands from a website to a
java program being ran on a computer.
This equivalent to asking if 2 programs can communicate via network.
The answer is yes. It is obvious, right? I mean how do you connect to your website in the first place? Eh?

Convert a Ruby on Rails project to a JRuby on Rails project

is there any particular way to convert a Ruby on Rails project to a JRuby on Rails project?
There now is a need to get the Rails side to talk to a Java server via RMI so was wondering how to make the conversion. Thanks.
As #Karl says, it pretty much works but there are a number of caveats you should be aware of:
The database adapters will be different from the ones you may be used to. They're based on JDBC. For example, the MySQL adapter install is described here
You will generally deploy to Java application servers in production such as Glassfish, Tomcat or Jetty. Typically this is done using Warbler
Certain gems with native extensions may not work. See here for details on the what is required
It pretty much just works, you simply run it with JRuby instead of Ruby.
You might be interested in this though:
http://jrubyist.wordpress.com/2009/07/15/jruby-and-sqlite3-living-together/

How to make my Java Swing application a Client-Server application?

I have made a Java Swing application. Now I would like to make it a Client-Server application. All clients should be notified when data on the server is changed, so I'm not looking for a Web Service. The Client-Server application will be run on a single LAN, it's a business application. The Server will contain a database, JavaDB.
What technology and library is easiest to start with? Should I implement it from scratch using Sockets, or should I use Java RMI, or maybe JMS? Or are there other alternatives that are easier to start with?
And is there any server library that I should use? Is Jetty an alternative?
Given that you have the application already, perhaps the simplest thing to do is to identify the interface that you require between the client and server, and first of all to refactor your application to use that interface to talk between the back-end/front-end within the same process.
Then you can start to split this apart. A simple solution would be to split this apart using RMI (since you're talking Java objects and have Java method calls). Spring contains useful tools to simplify/automate the RMI exposure of interfaces.
For the notification requirement, a simple UDP multicast (or broadcast) would suffice.
Note that as soon as you split your application up, you have issues re. maintaining consistent views of data, managing timely updates, handling cases when the server is down, possible loading issues when you get lots of clients etc. In a sense, splitting the application up into a client and server is just the start of a new architecture process.
Mina is a good choice as a network application framework for building a simple server for this purpose - it's a much better option than using raw sockets.
http://mina.apache.org/
If you really need an application server then you could take look at JBoss. It also provides a remoting component (as an alternative to something like Mina):
http://www.jboss.org/jbossremoting
You probably won't have much need for Enterprise Java Beans though. In most cases a simple POJO based framework is more than sufficient - you could tie this altogether with a dependency injection framework such as Guice:
http://code.google.com/p/google-guice/
or Spring. Keep it simple, don't use a J2EE server unless you really need to. Hope that helps.
This is much of what J2EE does, but it's a whole new learning curve because they have pre-solved many of the problems you will run into and many you may not and therefore add on a lot of new technologies.
But at it's most basic, J2EE answers just that question.
I worked in a project like this. We implemented Client-Side Swing and Server side with J2EE. We used EJB,Stateless beans and Message Driven Beans.Also I have been in a device tracking, management project. Our clients were trucks+Swing users and We have used Servets+TCP/UDP,Apache Mina framework to handle and keep connections.
I have been working in Java Swing Client/Server applications for almost 3 years. I would suggest you to go for RMI/EJBs. The initial application that we developed was doing this using RMI/EJB for client-server communication with WebLogic being the server.
But we later found out that there are lot of "browser-like" features to be included to the application such as session-timeout etc., So, we used the BrightSide Framework which wraps the RMI calls through HTTP. One more enhancment we made is that we replaced Weblogic with the open source JBoss server.
The wrapping of calls with HTTP will become very handy and you can make your swing applications really rich. Later, when the situation demands for you to use a website strictly, you can deploy your swing using jnlp.
Hope this helped.

Categories