Best way to instantiate a Singleton in a web app?

Posted on 2006-05-16
Last Modified: 2010-03-31
I have a POJO singleton that implements the observer pattern. I need it to register itself with a number of objects that implement observable. This is in a web (Struts) application. What's the best way to instantiate the singleton when the application starts up?
Question by:FeralCTO
    LVL 92

    Accepted Solution

    you could that it from a servlet and store it in the ServletContext
    LVL 1

    Author Comment

    So are you suggesting I implement a servlet that loads at startup and then instantiate my singleton in the init( ) method?
    LVL 92

    Expert Comment

    LVL 5

    Expert Comment

    Probably want to make the constructor on the singleton synchronized too.
    LVL 30

    Expert Comment

    No, I think its better to make the getInstance () synchronized. Anyway, we don't know how it is implemented - maybe it already is the right way.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    6 Experts available now in Live!

    Get 1:1 Help Now