Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm using ServerSocket for creating a server.
I must create a new thread for each client or is it possible&safe to use only the server's thread?
If you just use the server's main thread, then only one client will ever be able to connect, becuase the thread is busy with the only client connected. After that client disconnects, the server can take another client, but you want to be able to accept clients constantly. Thus you need to spawn a thread for each client.
If you use only the server thread you will be able to manage only one client for time. I suggest you to use Executors class in order to create a pool of thread. The pool thread can manage multiple connection like the ExecutorService javadoc shows.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
For example , I develop a connection pool as a server , and on the other application I want to use the connection existing in the pool , How can I get it ? they are in two processes ? and have different life circle ?
If you are talking about socket connections (i.e. your connection pool handles tcp connections), then you can't pass that connection from one process to another. However, you could have a connection from the second process to the server and relay the information to the second process (essentially acting as a proxy).
In general, you will need a way for the two processes to communicate. If they have a different lifecycle (which you hint at) and you need one process to pick up messages from the other process when it comes on line, then you will need a persistence and queuing mechanism as well. Depending on your needs there are many different ways to achieve this. Here are some examples: -
On the server, write the information to a socket and read it on the other process. You would use one of the Java messaging classes and might serialiaze the object information. This is non-persistent, but might be the easiest to begin with.
On the server, write the information to a file and signal either by a named semaphore, file or other means that there is information to be processed.
On the server, write the message to a guaranteed delivery queue (e.g. Amazon or Azure queue) so that it can be picked up by the other process when available.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am writing a system core (server) that is to handle multiple connections and requests. Lets suppose my server has a client connected and is ready to communicate. My question here is, how would i make the server know that client wants to send some data or reversal. Since read blocks until it receives data... so how to manage between the read and write operations to sockets if we don't know what the remote system at other end expects?
I can see a way around it using multithreading. Any way around please???
Multithreading is the only sensible way around this (in fact, it's one of the absolute classic cases where threading is required) - you'll have to create a new thread for each connection.
You may wish to do this directly, or you may wish to use the constructs available in java.util.concurrent (which I'd recommend) - thread pools for instance. One sensible approach might be to use a fixed thread pool to make sure the number of threads doesn't grow too ridiculously large, and then spawn threads off there as required.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is it possible to send a Java thread instance in an HTTP request?
No. A Java thread cannot be serialized, and therefore it cannot be sent outside of the JVM in which it was originally created.
(For a moment, just imagine what would be involved in moving a thread from one JVM to another. What about the static variables it might refer to? What about the other threads that it might to interact with? The problem is intractable.)
Actually I am working on clustered servers and want to execute my some threads(defined Jobs and other heavy task) on one server(called it back office node). So is there any way to do that.
OK, so that's a different problem. You actually need to execute tasks remotely, not pass threads. ('Cos you can't pass threads.)
Yes, there are many ways to send tasks to be executed elsewhere ... in the general sense. What you need to do is to express the task or task description in a form that can be serialized in some way (Java Object serialization, JSON, XML, etcetera) and then pass the serial form to the server that is going to execute it. The server would be responsible for managing the thread that does the execution.
Caveat: This is not going to work well if the task is needs to refer to a whole lot of data / objects on the machine where you formulated the task. That is going to involve serializing / passing all of the data OR figuring out some way for the backend to "call back" to get the specific data items that a task needs.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
My application is installed on a customer linux machine as a service.
From time to time he complains that the application stops.
The thing is that I can see from my application logs that the service is stopped gracefully (not crashed), but he's saying he didn't stopped it.
How can I tell who caused my service to stop?
My application listen to a configured port via socket, if someone writes to this socket - the application stops.
The customer say that there's no automated process that might cause the service to stop.
If it is an actual graceful shutdown, the culprit can be found by looking in the history. If it's the fault of the system, you should be able to correlate the time stamp of your application shutdown, with an event in the sys logs. If it's caused by something external, you might want to increase your logging on incoming connections.
This could be because of some extreme resource starvation. Ask your client if he is running out of RAM and swap space.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Can you please explain how singleton is use full for creating database connection?
How it will work in multithreaded environment while creating db connection and closing the connection? Once connection is closed will it gets disconnect from database?
You should normally use a "connection pool" when managing database connections in your Java applications. Creating connections is expensive / heavy so you really don't want to be creating them time and time again, especially for a busy site (the performance drop will kill it).
The manner in which you acquire the pool reference depends on the type of application (managed like servlet/JSP or a standalone application) but search around for "datasource". Also have a look at this answer.