users@glassfish.java.net

Re: _at_EJB annotation nightmare

From: Alexis Moussine-Pouchkine <alexis.mp_at_sun.com>
Date: Tue, 15 Jun 2010 10:13:33 +0200

I'm not sure why you want to decouple RSSVersion and TimerService.
Both are singletons and you could simply place the updateReportBase() method in RSSVersion.java

Even when keeping them separate, you can do without the local interfaces and the "name" attribute of the @EJB annotation.
Here's a simplified version :

package pl.brag.beans;

import javax.ejb.Singleton;

@Singleton
public class RSSVersion {
    private long rssVersion = 0L;

    public String getCurrentVersion() {
        return String.valueOf( rssVersion );
    }

    public void setCurrentVersion(long newVersion) {
        if( newVersion > rssVersion ) {
            rssVersion = newVersion;
        }
    }
}

-----

package pl.brag.beans;

import javax.ejb.*;

@Singleton
public class TimerService {
    @EJB
    private RSSVersion rssVersion;

    @Schedule( minute="*/1", hour="*" )
    public void updateReportBase() {
        System.out.println( "Timer called" );
        System.out.println( rssVersion.getCurrentVersion() );
    }
}


-----

public class HelloServlet extends HttpServlet {

    @EJB RSSVersion rssVersion;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet HelloServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("Current version = " + rssVersion.getCurrentVersion() );
            rssVersion.setCurrentVersion(...);
            ...

-----

All packaged into a single WAR archive :
        WEB-INF/classes/pl/brag/beans/RSSVersion.class
        WEB-INF/classes/pl/brag/beans/TimerService.class
        WEB-INF/classes/servlets/HelloServlet.class

-Alexis


On 15 juin 2010, at 09:52, glassfish_at_javadesktop.org wrote:

> Hi I am working on Glassfish v3 and cannot inject my ejb into some object.
> I tried many times and changed code but no use.
> I give you my netbeans enterprise project.
> There are two beans RSSVersion which holds version of some RSS and TimerService which is invoked periodically and prints rss version.
> For this to work I must inject RSSVersion object into TimerService singleton..
> Please tell me what I am doing wrong.
> [Message sent by forum member 'glesiak']
>
> http://forums.java.net/jive/thread.jspa?messageID=474293
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>