Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: [HELP] Java Context Class Loader Exmaple / Setup

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [HELP] Java Context Class Loader Exmaple / Setup

    Hi,

    I am in process of developing a Java application (war) which will be deployed in an Application Server like jBoss, WebSphere, WebLogic and Glassfish. Within my code I am getting an instance of a class which is 'static', now If I deploy two instances of the same application, both will end up using same static instance rather than different. Is there anyway I can have only one instance of a class for each separately deployed application without using 'static' property. I heard about this 'Context Classloading Stuff' but couldn't grasp the idea. Can anyone explain or refer me to the good article about it. Also, I am looking for an example, seems like its all about configuration of my application at deployment time but how?

    I am quite new to java so please accept my apology if I am talking nonsense.

    Thanks,

    --

    SJunejo


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: [HELP] Java Context Class Loader Exmaple / Setup

    If I deploy two instances of the same application, both will end up using same static instance rather than different
    What do you mean by "deploy" an instance?
    Are you loading all of the classes for an application in the same JVM?

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [HELP] Java Context Class Loader Exmaple / Setup

    I mean copy of the same application with different settings....I think jBoss load's all *.war in single Web Container?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: [HELP] Java Context Class Loader Exmaple / Setup

    copy of the same application with different settings
    Are you talking about disk files copied into another disk file (like a jar)
    or about the bytes of a class file loaded into the jvm where they can be executed.

    What is it that is "copied"? And where is it copied to?

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [HELP] Java Context Class Loader Exmaple / Setup

    OK....I think there is something I have missed. Let me explain, I have an application called ABCLive.war and ABCTest.war (both files copies of each other) and configured to use some resources from Application Server. ABCLive.war is configured to use 'jca/abcConnectionFactoryLive' and ABCTest.war is configured to use 'jca/abcConnectionFactoryTest' resources present in an application server. As I mentioned earlier both of the applications have same classes, same servlet and everything but uses different bindings in application server deployment descriptor i.e jboss-web.xml.

    The class which is responsible to perform lookup to these resources within my application is called 'ServiceLocater' which is implemented as 'singleton' instance. If I start using my 'ABCTest' application it will work OK and uses correct cx-factory i.e. 'jca/abcConnectionFactoryTest' without any problem, but now if I try to use ABCLive deployment it will end up using same jca resource as Test deployment instead of using 'jca/abcConnectionFactoryLive'.

    I found out that jboss will load both apps in the same web container and because of that both instances are end-up using single instance of ServiceLocater. Now what I want to know is that is there anyway I can achieve this singleton pattern without making it static.

    Hope this makes a bit more sense.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: [HELP] Java Context Class Loader Exmaple / Setup

    The singleton needs a place to keep a reference to the single instance it defines.
    A static is one way. Perhaps there are other places to keep the single reference.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: [HELP] Java Context Class Loader Exmaple / Setup

    Here's an idea:
    If you want a different version of the singleton class for each "environment", use a static Map to hold the reference to the class instance for each environment.
    No idea how to detect which environment the singleton is in.

Similar Threads

  1. [SOLVED] Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet
    By chronoz13 in forum File I/O & Other I/O Streams
    Replies: 14
    Last Post: July 9th, 2011, 11:14 AM
  2. Relative path issue with Context path struts
    By chinnu in forum Web Frameworks
    Replies: 1
    Last Post: March 31st, 2011, 10:17 AM
  3. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  4. How to setup PHP on Netbeans 6.8? Please help.
    By marksquall in forum Java IDEs
    Replies: 0
    Last Post: January 10th, 2010, 10:56 PM
  5. How to setup a server-multiple- clientenvironment
    By jigoku in forum Java Networking
    Replies: 5
    Last Post: October 29th, 2009, 09:19 AM