从EJB3访问ServiceBean

时间:2011-09-20 11:52:13

标签: ejb-3.0 jboss6.x

我实现了这个tutorial,但我无法从EJB3访问该服务。

我的ejb看起来像这样:

@Stateless
public class Authentication implements AuthenticationRemote {

  private ServiceTwoManagement man;

  ...    

}

@EJB将抛出:java.lang.RuntimeException: Could not resolve @EJB reference:

任何建议

1 个答案:

答案 0 :(得分:0)

你们没有机会发布具体的答案(隐藏了许多信息),我目前发现了一个干净的解决方案或一个错误的解决方法。在第二种情况下:我很高兴看到像我找到的更好的解决方案。

我的解决方案是创建一个service-bean(第三个bean)来保存所有的TransportObjects。

@Service 定义了一个Singleton,所以我使用Singleton-Pattern接近GoF-Pattern。

最后我的解决方案如下:

package org.gegenakw.ejb.user;

import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.logging.Logger;

import javassist.NotFoundException;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.PersistenceContext;

import org.antiakw.exceptions.authentication.AccountAlreadyExistsException;
import org.antiakw.exceptions.authentication.IllegalLoginException;
import org.antiakw.exceptions.authentication.NotYetActivatedException;
import org.gegenakw.Utils;
import org.gegenakw.ejb.extern.RegistrationEmailsTransportObjectHolder;
import org.gegenakw.ejb.interfaces.RegistrationTransferObject;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.hibernate.validator.InvalidStateException;
import org.jboss.ejb3.annotation.Depends;

import de.antiakw.jpa.beans.Benutzer;

/**
 * Session Bean implementation class Authentication
 */
@Stateless
public class Authentication implements AuthenticationRemote {

  public static Logger log = Logger.getLogger("Authentication");

  @PersistenceContext(unitName = "gegenakwjpa")
  Session hibernateSession;

  // @EJB
  private List<RegistrationTransferObject> registration = RegistrationEmailsTransportObjectHolder.getInstance();

  @Override
  public void createAccountCheckDuplicates(String email, String pass)
      throws InvalidStateException, AccountAlreadyExistsException {
    ...
  } 

  ...
}

我的RETOH看起来像这样:

package org.gegenakw.ejb.extern;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.gegenakw.ejb.interfaces.RegistrationTransferObject;
import org.jboss.ejb3.annotation.Management;
import org.jboss.ejb3.annotation.Service;

@Service(objectName = "gegen-akw:service=RETOH")
@Management(List.class)
public class RegistrationEmailsTransportObjectHolder extends ArrayList<RegistrationTransferObject> {

  private static RegistrationEmailsTransportObjectHolder me;

  public static RegistrationEmailsTransportObjectHolder getInstance() {
    return me;
  }

  @PostConstruct
  public void create() throws Exception {
    me = this;

  }

}

我的工作看起来像这样:

package de.antiakw.service.email;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.Security;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.mail.smime.SMIMEException;
import org.bouncycastle.mail.smime.SMIMESignedGenerator;
import org.gegenakw.ejb.interfaces.RegistrationTransferObject;
import org.jboss.ejb3.annotation.Depends;
import org.jboss.ejb3.annotation.ResourceAdapter;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

@ResourceAdapter("quartz-ra.rar")
@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "cronTrigger", propertyValue = "0 * * * * ?") }

)
public class RegistrationEmails implements Job {

  public static final Logger log = Logger.getLogger("RegistrationEmails");

  private static java.security.cert.Certificate certificate;

  @Depends("gegen-akw:service=RETOH")
  private List<RegistrationTransferObject> registrationMailHolder;

  @Resource(mappedName = "java:Gegen-AKW-Mailsystem")
  javax.mail.Session mailSession;

  @Override
  public void execute(JobExecutionContext arg0) throws JobExecutionException {
    synchronized (registrationMailHolder) {

@JavaKaBaby:为了满足你的需求,我使用@EJB instad of singleton-pattern发布我的stactrace(我希望这个问题与我的解决方法完全相同):

09:51:21,577 ERROR [AbstractKernelController] Error installing to Real: name=vfs:///C:/jboss-6.1.0.Final/server/default/deploy/gegen-akw.ear state=PreReal mode=Manual requiredState=Real: org.jboss.dep
loyers.spi.DeploymentException: Error during deploy: org.jboss.metadata.ejb.jboss.JBossEnterpriseBeanMetaData.Authentication
        at org.jboss.deployers.spi.DeploymentException.rethrowAsDeploymentException(DeploymentException.java:49) [:2.2.2.GA]
        at org.jboss.deployers.plugins.deployers.DeployerWrapper.deploy(DeployerWrapper.java:185) [:2.2.2.GA]
        at org.jboss.deployers.plugins.deployers.DeployersImpl.doDeploy(DeployersImpl.java:1832) [:2.2.2.GA]
        at org.jboss.deployers.plugins.deployers.DeployersImpl.doInstallParentFirst(DeployersImpl.java:1550) [:2.2.2.GA]
        at org.jboss.deployers.plugins.deployers.DeployersImpl.doInstallParentFirst(DeployersImpl.java:1571) [:2.2.2.GA]
        at org.jboss.deployers.plugins.deployers.DeployersImpl.doInstallParentFirst(DeployersImpl.java:1603) [:2.2.2.GA]
        at org.jboss.deployers.plugins.deployers.DeployersImpl.install(DeployersImpl.java:1491) [:2.2.2.GA]
        at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:379) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:2044) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:1083) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractController.executeOrIncrementStateDirectly(AbstractController.java:1322) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1246) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1139) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:939) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:654) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.deployers.plugins.deployers.DeployersImpl.change(DeployersImpl.java:1983) [:2.2.2.GA]
        at org.jboss.deployers.plugins.deployers.DeployersImpl.process(DeployersImpl.java:1076) [:2.2.2.GA]
        at org.jboss.deployers.plugins.main.MainDeployerImpl.process(MainDeployerImpl.java:679) [:2.2.2.GA]
        at org.jboss.system.server.profileservice.deployers.MainDeployerPlugin.process(MainDeployerPlugin.java:106) [:6.1.0.Final]
        at org.jboss.profileservice.dependency.ProfileControllerContext$DelegateDeployer.process(ProfileControllerContext.java:143) [:0.2.2]
        at org.jboss.profileservice.dependency.ProfileDeployAction.deploy(ProfileDeployAction.java:151) [:0.2.2]
        at org.jboss.profileservice.dependency.ProfileDeployAction.installActionInternal(ProfileDeployAction.java:94) [:0.2.2]
        at org.jboss.kernel.plugins.dependency.InstallsAwareAction.installAction(InstallsAwareAction.java:54) [jboss-kernel.jar:2.2.0.SP2]
        at org.jboss.kernel.plugins.dependency.InstallsAwareAction.installAction(InstallsAwareAction.java:42) [jboss-kernel.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.action.SimpleControllerContextAction.simpleInstallAction(SimpleControllerContextAction.java:62) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.action.AccessControllerContextAction.install(AccessControllerContextAction.java:71) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractControllerContextActions.install(AbstractControllerContextActions.java:51) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:379) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:2044) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:1083) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractController.executeOrIncrementStateDirectly(AbstractController.java:1322) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1246) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1139) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:939) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:654) [jboss-dependency.jar:2.2.0.SP2]
        at org.jboss.profileservice.dependency.ProfileActivationWrapper$BasicProfileActivation.start(ProfileActivationWrapper.java:190) [:0.2.2]
        at org.jboss.profileservice.dependency.ProfileActivationWrapper.start(ProfileActivationWrapper.java:87) [:0.2.2]
        at org.jboss.profileservice.dependency.ProfileActivationService.activateProfile(ProfileActivationService.java:215) [:0.2.2]
        at org.jboss.profileservice.dependency.ProfileActivationService.activate(ProfileActivationService.java:159) [:0.2.2]
        at org.jboss.profileservice.bootstrap.AbstractProfileServiceBootstrap.activate(AbstractProfileServiceBootstrap.java:112) [:0.2.2]
        at org.jboss.profileservice.resolver.BasicResolverFactory$ProfileResolverFacade.deploy(BasicResolverFactory.java:87) [:0.2.2]
        at org.jboss.profileservice.bootstrap.AbstractProfileServiceBootstrap.start(AbstractProfileServiceBootstrap.java:91) [:0.2.2]
        at org.jboss.system.server.profileservice.bootstrap.BasicProfileServiceBootstrap.start(BasicProfileServiceBootstrap.java:132) [:6.1.0.Final]
        at org.jboss.system.server.profileservice.bootstrap.BasicProfileServiceBootstrap.start(BasicProfileServiceBootstrap.java:56) [:6.1.0.Final]
        at org.jboss.bootstrap.impl.base.server.AbstractServer.startBootstraps(AbstractServer.java:827) [jboss-bootstrap-impl-base.jar:2.1.0-alpha-6]
        at org.jboss.bootstrap.impl.base.server.AbstractServer$StartServerTask.run(AbstractServer.java:417) [jboss-bootstrap-impl-base.jar:2.1.0-alpha-6]
        at java.lang.Thread.run(Thread.java:662) [:1.6.0_25]
Caused by: java.lang.RuntimeException: Could not resolve @EJB reference: [EJB Reference: beanInterface 'java.util.List', beanName 'null', mappedName 'null', lookupName 'null', owning unit 'ComponentDe
ploymentContext@848640405{org.jboss.metadata.ejb.jboss.JBossEnterpriseBeanMetaData.Authentication}'] for environment entry: env/org.gegenakw.ejb.user.Authentication/registration in unit ComponentDeplo
ymentContext@848640405{org.jboss.metadata.ejb.jboss.JBossEnterpriseBeanMetaData.Authentication}
        at org.jboss.ejb3.jndi.deployers.resource.provider.AnnotatedEJBRefResourceProvider.provide(AnnotatedEJBRefResourceProvider.java:99) [:0.1.7]
        at org.jboss.ejb3.jndi.deployers.resource.provider.AnnotatedEJBRefResourceProvider.provide(AnnotatedEJBRefResourceProvider.java:50) [:0.1.7]
        at org.jboss.switchboard.mc.JndiEnvironmentProcessor.process(JndiEnvironmentProcessor.java:68) [:1.0.0-alpha-15]
        at org.jboss.switchboard.mc.deployer.AbstractSwitchBoardDeployer.process(AbstractSwitchBoardDeployer.java:119) [:1.0.0-alpha-15]
        at org.jboss.switchboard.mc.deployer.EJBEnvironmentSwitchBoardDeployer.internalDeploy(EJBEnvironmentSwitchBoardDeployer.java:87) [:1.0.0-alpha-15]
        at org.jboss.deployers.spi.deployer.helpers.AbstractRealDeployer.deploy(AbstractRealDeployer.java:55) [:2.2.2.GA]
        at org.jboss.deployers.plugins.deployers.DeployerWrapper.deploy(DeployerWrapper.java:179) [:2.2.2.GA]
        ... 45 more