0

I am implementing a @singleton where inside i would like to have a scheduled function.

 @Singleton
 public class MyClass
 {
     static volatile MyClass instance;
     private Map<String, String> myArray = new HashMap<String, String>();
     private MyClass()
     {
     }

     public static MyClass getInstance()
     {
         if (instance == null)
         {
            synchronized (MyClass.class)
            {
                if (instance== null)
                {
                    instance= new MyClass();
                }
            }
        }
        return instance;
     }


    public void setPar( String key, String value)
    {
        this.myArray.put(key, value);
        for (Map.Entry<String, String> entry : this.myArray.entrySet() )
        {
            System.out.println( entry.getKey()+ " ::: " + entry.getValue() );
        }
    }

    @Schedule(second = "*/5", minute="*",hour="*", persistent=true)
    public void doJob()
    {
        System.out.println("*** Hello from scheduled task ***");
    }
}

This is inside a Dynamic web Project created in eclipse and run in a Tomcat server.

So, when i make a request for an html file a servlet is executed.

@Stateless
public class MyServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    @Override 
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        String param1= req.getParameter("param1");
        String param2= req.getParameter("param2");
        MyClass inst= MyClass.getInstance();
        inst.setPar(param1, param2);
    }
}

What i would like is for the doJob() to be executed using the @Schedule annotation.

I was guessing that since the MyClass instance is loaded -and by printing the array value, it surely works- that the doJob() would also start and work as described in the @Schedule.

Is there something wrong with any configuration of the project? I tried adjusting with this but nothing changed. What am i doing wrong?

Thanks in advance for any help. (I am fairly new to Java so be gentle).

Community
  • 1
  • 1
0gap
  • 21
  • 7
  • I have also tried setting persistent=false and also annotating the MyClass @Stateless and also altering the import from javax.ejb.Singleton to javax.inject.Singleton. as mentioned in other posts, but nothing changed. – 0gap Jan 14 '16 at 11:31
  • 1
    You need to use a Java EE server such as TomEE, JBoss/WildFly, Glassfish, etc in order to do this. Tomcat does not provide the services you need. – Steve C Jan 14 '16 at 12:34

0 Answers0