| D:\DEV\PROJECTS\Harbor_Tests\src\harbor\test\testSingletons.java |
/* * testSingletons.java */ package harbor.test; import harbor.interfaces.*; import kewlstuff.harbor.client.Vessel; /** * @author Johnny Kewl * TEST 7 * Singleton Test */ public class testSingletons { private Vessel vessel = null; /** Creates a new instance of testSingletons */ public testSingletons() { String harborUrl = "http://localhost:8080/harbor/service"; //location of container harbor vessel = new Vessel(harborUrl); //get a vessel from the harbor } public void runTest(){ //You need to run this test several times to see it working I_ParamHolder i_ParamHolder = (I_ParamHolder)vessel.loadRemoteSingleton(I_ParamHolder.class,"harbor.classContainer.ParamHolder","MARY");//load a class instance ON the harbor if(i_ParamHolder == null){ System.out.print("Class does not exist or connection problems\n"); return; } //This adds data to the existing data String dataInPool = i_ParamHolder.getData(); dataInPool += " 20 kisses :)"; i_ParamHolder.setData(dataInPool); System.out.print(dataInPool + "\n"); //You dont release singletons but notice that you can release it if you want to. //vessel.releaseRemoteClassInst(i_ParamHolder); } } /* Notes: * In general, you dont want to use singletons, but sometimes you have to. * A singleton is a class shared by clients... there is only one instance * of it. * It has an ALIAS name so that all clients can find it. * So if I called it MARY and gave her 20 kisses. * And you did the same... then if either of us asked her how many times * she got kissed, it would be 40. We share MARY. * * You normally dont release a singleton because the idea is that its shared. * Only the super_admin and the url client that created it, can release it. * * An important thing to remember is that in singletons you must write * your code for multiple threads, because we could both kiss MARY at the same time. * Typically you will use synchronize in your methods. * * The term singleton is used loosely, in most respects its just a normal class * that we choose to make a singleton. * * What do you use this for?... things like database pools, or sharing a message between * clients. * * Typically in a database pool, you would load up the pool as a singleton * and then inject it into any other class that needs it. * See the testControllingRemoteObjects for how to inject one class into another. * * In the /harbor/admin console, if you look at the active classes, its easy to * tell which ones are singletons and which ones are normal classes that have not been * released... the one has an ALIAS name, the other does not. * Before releasing a class you think may be abandoned, refresh the browser and in many * cases it will dissappear. The browser may just catch a snap shot of a * remote client starting a class... to make it easy to decide, the time the class * has been active is also shown, so if its not a singleton and two days * old... its safe to remove. * * From a container perspective you probably very familiar with JNDI or some other mechanism * that provides a database pool. You can continue doing whatever you like doing, * its POJO, you are free. The idea however is that one develops outside the container because * there is no better way to proof and debug an application than when it operates as a * a whole standalone entity. * So one class in your application is a database pool, and the other classes use it. * Simply by starting that class as a singleton, all the other applications can use it * as well, its not initialized and destroyed by each application, and thus dB connections * dont drop and remake. Naturally when you develop other applications externally you will * use the same dBpool as a library for those applications, and even though, externally * you cant start the dBpool as a singleton, ie the class will close when the application * closes, inside Harbor... its a shared resource when started as a singleton. */