cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
42
Views
0
Helpful
0
Comments
cdnadmin
Community Member
This document was generated from CDN thread

Created by: Janine Graves on 20-07-2006 02:00:03 AM
This was posed by a prospective CVP student: can you help?

How to call a specific method of a java class from a CVP componet, instead of creating an instance of the class each time one of it's methods is needed.

For example, If we want to establish a communication to an external server via java sockets, we should:

-Create an instance of the java class that implements the sockets communication.

-Write to the socket via a method of the class.

-Read from the socket via a method of the class.

But what about if we want to write and read at different points in the application (I mean from diffrent components) employing the same socket instance that was created before.

¿How can a java object instance be accesible at different points in the application? We are interested in invoking certainly methods of the class with out creating a new instance each time we employ one of its method, because we want to employ the same socket connection.

The CVP components could be configured to use a java class, but not a simple method.

Thanks, Janine

Subject: RE: Java question
Replied by: null on 21-07-2006 10:41:20 PM
Hi Janine,

There are several ways you can do this:

1) The first is to simply put the instance of the Java object that holds the connection in Session Data. That way any component that wishes to use that object can without having to recreate it. There are two disadvantages to this. The first is that this will last only as long as the session itself, requiring the connection to be re-established at the start of each call. This may actually be the desired behavior. The other disadvantage is that when using clustering or session replication, this object would not be replicated because a connection is not serializable. This isn't too much of a big deal if session replication or clustering is not used, and even if it is, the code could be written such that if a null was found in the Session Data where the connection would have been stored, it would re-establish the connection.

2) Make the class a singleton. In a singleton, one static reference to a connection object is stored and can then be shared by any component. You do not need to recreate the connection each time, every component can then access it. There is one disadvantage to this. This object would then be accessible from all calls to the application if it were deployed within the application's java folder or all calls to all applications if it were deployed to the common folder. This may be an issue if the intention is to keep this connection only for a single call. You could work around this by keeping a static HashMap of connections where the key to the HashMap is the session ID of the call. You create the connection and add to that HashMap in the start of call class and remove the connection in the end of call class. That way you can keep separate connections for each session. One last thing to note would be that if the application were updated, this singleton object would be reset to null, though the singleton design pattern can handle this contingency as well.
Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Quick Links