JavaEE 6中的异步方法

时间:2015-12-20 10:55:18

标签: asynchronous java-ee-6

我尝试在JavaEE 6中使用异步方法。我创建了以下类。但是,输出日志的结果是同步。这不对。

无国籍班

   @javax.ejb.Stateless
    public class MailManager implements Serializable{
    @Asynchronous
        public void send(List<String> toList, Mail mail, final SystemSetting systemSetting) throws MailException, UnsupportedEncodingException{

            boolean isError = false;
            for(InternetAddress addr :addressList){
                    MimeMessage msg = getMimeMessage(addr, mail, session);
                    Transport.send(msg);
                    log.info("Sent email to ["+addr+"].");
            }
            if(isError){
                throw new MailException("Mail sending error occurred. Check error log.");
            }
            log.debug("Async finish.");
        }
    }

ManagedBean

@Named
@SessionScoped
public class MailController extends AbstractController{
    @Inject @new(MailCreater.class)
    private MailFactory mailCreater;

    @Inject
    private MailManager mailManager;

    public void send(){
        Mail mail = null;
            mail = mailCreater.createFromTemplate(this, false,
                    mimeInfo, "template/testTemplate.txt","template/FOOTER1.body", map);
            mailManager.send(list, mail, systemSetting);
        log.debug("send() finish.");
    }
}

日志

INFO[Log4jLogger] - Sent email to [test1@test.co.jp].
INFO [Log4jLogger] - Sent email to [test2@test.co.jp].
.
.
.
DEBUG [Log4jLogger] - Async finish.
DEBUG [Log4jLogger] - send() finish.

更新

将@Inject更改为@EJB

时的错误代码
warning : javax.ejb.EJBException: javax.ejb.EJBException:
javax.ejb.CreateException: Could not create stateless EJB
        at
com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:454)
        at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2547)
        at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1899)
        at com.sun.ejb.containers.EjbAsyncTask.call(EjbAsyncTask.java:99)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)
Caused by: javax.ejb.EJBException: javax.ejb.CreateException: Could not
create stateless EJB
        at
com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:726)
        at
com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:247)
        at
com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:449)
        ... 7 more
Caused by: javax.ejb.CreateException: Could not create stateless EJB
        at
com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:534)
        at
com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:95)
        at
com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:724)
        ... 9 more
Caused by: java.lang.NullPointerException
        at java.util.concurrent.ConcurrentHashMap.hash(ConcurrentHashMap.java:333)
        at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:988)
        at
org.jboss.weld.manager.BeanManagerImpl.getBean(BeanManagerImpl.java:1076)
        at org.jboss.weld.manager.BeanManagerImpl.getBean(BeanManagerImpl.java:148)
        at
org.glassfish.weld.services.JCDIServiceImpl._createJCDIInjectionContext(JCDIServiceImpl.java:169)
        at
org.glassfish.weld.services.JCDIServiceImpl.createJCDIInjectionContext(JCDIServiceImpl.java:146)
        at
com.sun.ejb.containers.BaseContainer.createEjbInstanceAndContext(BaseContainer.java:1639)
        at
com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:475)
        ... 11 more

0 个答案:

没有答案