I have created a Java Web Application that asks the user to upload an ARF File, converts it to MP4 and saves it on the server and the user is then sent an email with the link of the new MP4 file. The conversion takes place by calling a VB Script from the command line. This script converts all the ARF files that are located in a desired directory to MP4 format.
The application has been working well but I have noticed one thing. It does not work when one person (using one computer) runs the application and say the file is in the process of being converted (i.e. the user is waiting for the email). At this stage if another user (using another computer) tries to run the application at the same time, the conversion process gets disrupted and it stops the previous conversion.
I tried testing the VB Script by running it, waiting for a video to be converting and then running it again. I noticed the same thing.
Was wondering if there is a way to make my application concurrent so that it can run independently i.e. more than one person can run it at the same time.
Thank you
The limitation here is the limitations of the VB script. Can you (for example) generate a different filename for each so that it works in parallel?
If not then you will have to queue up the requests and then have some code that processes the next request from the queue one at a time.
It sounds like the code that invokes the VB script is the bottleneck. I take it that the file conversion process would take some amount of time so that what you want is have the processing started in a background thread and terminate the java session. The first thing that came to my mind is Quartz job framework. You can trigger a Quartz job that does the processing and emailing and each Quartz job is running on its own thread. http://quartz-scheduler.org/documentation
Related
I made a simple chat room application consists of a client.jar and server.jar. After running the .jar file in cmd window, I need to type in some specified command in client part and carriage return to run the application, such as "#connect": connect to the server, "#send XXX": send XXX message to server and broadcast to other connected clients...
Here comes the problems. Actually I want to test how the application works well when there exist concurrency. So I think I need try to let two or more client parts send the command repeatedly and fast. But obviously i cannot achieve manually.
Is there any ways can let the system get the command and send it automatically at high speed? I guess using batch file is suitable for this issue, but I am not sure or how to implement it...
if we want to system level one daemon program running continuously to listen the creation/modification/access of files & save this information in log.Do we have any concept to implement this?
The required creation/modified/ access of file are set of file attributes stored with the file itself. So you can simply run a program which iterate through all the files in a give directory and read the required attributes. This program can be scheduled as a CRON job or Windows scheduled task so that it runs for every 5mins, 10mins, etc...
http://www.codeproject.com/Questions/172608/Getting-file-details-using-java is how you can read those attributes.
I have created and am working on a server-application that monitors for specific folders and takes appropriate actions whenever files are being added.
Now I come to the point where I want to be able to shutdown the program, for example for applying a patch.
The server runs simply in a command prompt, how can I signal that I want to perform maintenance on it? I do not think reading System.in is feasible as I am also outputting text in the prompt.
Regards.
You could try reading System.in as System.in and System.out are different file descriptors. What this means is that by writing things in console you are not writing in the same place than when you are typing, so console output should not matter for reading commands in the prompt.
A second application can be used to communicate with the server application. You can use Java Management Extensions or implement your own client/server communication using sockets.
Another way to achieve this is that server periodically checks for existence of an specific file somewhere on hard disk. If server finds that specific file, it will shut down.
I recently implemented Java 7's WatchService and it works perfectly. Now I wondered if there is a way to get all the Events which occured since the last run of my program. For example:
I run my program, create some files, edit some files and I get all the corresponding Events
I close my program
I create a file named foo.txt
I start my program, and the first event i get is an ENTRY_CREATE for foo.txt
I thought about saving the lastModifiedDate and searching for files and directorys newer than the last execution of my program. Is there another (and better) way to do this?
There is no better way to do this if your program is meant to scan for all file changes (apart from storing files in a content / source control repository, but that would be external to your program).
Java 7's WatchService is only a more performant way than continuously looping and comparing file dates / folder contents, hence you need to implement your own logic to solve your own problem.
There is no way to do this in Java, or in any other programming language.
The operating system doesn't (and can't) buffer file system events on the off-chance that someone might start a program to process them. The event monitor / delivery system captures the events for a running application that is listening for them. When nothing is listening, the events are not captured.
You could write a small daemon (system service on Windows) which runs continuously and listens for file system changes. It could write these to a file. When your application runs, rather than listening for changes itself, it could just read the file. As events happen while it runs, the daemon will continue to receive them and send them through the file to the application.
You would need to ensure that the file was organised in such a way that it could be written to and read from safely at the same time, and that it did not grow indefinitely.
Is there a way I can start a command-line application from java and then send strings (commands) to its input stream and display its response from its output stream?
I'm using an application with a pretty sophisticated command line interface (vlc). The application has an interpreter that responds to a set of commands. For example, after I start the app, I can start or stop a movie by issuing the command 'pause' on the command line.
I'd like to write a java application that executes the program and issues commands to the program. I've seen many examples of java apps starting an application and getting the output stream of the app displaying the output of the app. But I've never seen an example, in which the java app would send requests to the sub-application.
Is there a way I can do this using java?
Thanks in advance!
So long as the spawned process listens on stdin for input, sure.
You'd launch a Process in the usual way (Runtime.exec()) - I won't document it here, as you say you've seen plenty of examples.
Then once you have a handle to the spawned process, you call the confusingly-named getOutputStream. This gives you an OutputStream, the other end of which is connected to the process' standard input. Hence, any bytes written to this stream can be consumed by your child process, just as if you were typing/piping input from a console.
I will point to a couple of resources that are always worth reading when dealing with Processes; cut-and-paste jobs from arbitrary Google results often don't cover the edge cases properly and can lead to deadlocks:
When Runtime.exec() won't (old, but still relevant)
Five common Java Process pitfalls