将 EJB 2.0 迁移到 EJB 3.x Web 应用程序会忘记较低的层值



我目前正在尝试迁移一个 EAR 项目
-旧的项目-
EJB 2.0
Jboss 5.0.1

-新项目-
EJB 3.0
野蝇 13.0.0最终版

它的会话豆子,我已经设法创造了,我可以调用它。 它本身的逻辑似乎有效。我遇到的问题是其中一个后续似乎忘记了它在做什么.
我看到的当前问题如下:
在一个类的 Web 应用程序中,我们正在创建一个另一个类的对象,然后我们触发它的父方法。然后,此父方法调用一个类,并将其 self 作为参数,然后检查它是哪种类型,然后根据哪种类型为会话 Bean 加星标。然后调用参数函数performExecute()在此函数中我们调用查询并实际获得正确的结果,然后将结果集值添加到私有 DTO 成员。然后执行执行完成。我们回到了 Web 应用程序类,然后我们尝试使用 get 函数访问相同的 DTO 成员。这将返回一个空指针。我想知道我是否忘记了会话豆子中的内容?

旧会话豆:

public class TxNotSupportedCommandServerBean implements SessionBean {
SessionContext sessionContext;
public void ejbCreate() throws CreateException {}
public void ejbRemove() {
sessionContext = null;
}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public void executeCommand(TargetableCommand cmd) throws CommandException {
try {
cmd.performExecute();
}
catch (CommandException ex) {
throw ex;
}
}
}

新的:

@Stateless
@Remote
@TransactionManagement(value=TransactionManagementType.CONTAINER)
@TransactionAttribute(value=REQUIRED)
public class TxNotSupportedCmdServerBean  implements TxNotSupportedCmdServerRemote{
/**
* Default constructor. 
*/
public TxNotSupportedCmdServerBean() {
// TODO Auto-generated constructor stub
}

public void executeCommand(TargetableCommand cmd) throws CommandException {
try {
cmd.performExecute();
}
catch (CommandException ex) {
throw ex;
}
}
}

这两者都在 EJB 中。Jar
接口在 EJBClient.jar
旧接口中实现:

public interface TxNotSupportedCommandServerLocal extends EJBLocalObject {
public void executeCommand(TargetableCommand cmd) throws CommandException;
}

新界面:

public interface TxNotSupportedCmdServerRemote {
public void executeCommand(TargetableCommand cmd) throws CommandException;
}

现在下面是下一组文件,它也在 EJBClient.jar
TargetableCommand 中:

public abstract class TargetableCommand implements Command {
private boolean constraintViolated;      
protected RequestContext requestContext; 
protected String dataSourceName;         

public TargetableCommand(RequestContext requestContext, String dataSourceName) {
this.requestContext = requestContext;
this.dataSourceName = dataSourceName;
}
public TargetableCommand(RequestContext requestContext) {
this.requestContext = requestContext;
}
public TargetableCommand(String dataSourceName) {
this.dataSourceName = dataSourceName;
}
public TargetableCommand() {
}
public void setConstraintViolated(boolean constraintViolated) {
this.constraintViolated = constraintViolated;
}
public boolean isConstraintViolated() {
return constraintViolated;
}
public abstract void performExecute() throws CommandException;
public void execute() throws CommandException {
CommandTarget.executeCommand(this);
}
}

命令:

public interface Command extends Serializable {
public void execute() throws CommandException;
}

注释的代码是旧的会话 bean.
CommandTarget:

public class CommandTarget {
public CommandTarget() {
}
/**
* Exekverar ett kommando i rätt miljö, t.ex. med eller utan transaktionshantering
* @param cmd TargetableCommand Kommandot som ska utföras
* @throws CommandException
*/
public static void executeCommand(TargetableCommand cmd) throws CommandException {
Context context = null;
try {
ServiceLocator sl = ServiceLocator.getInstance();
//      if (cmd instanceof TxRequired) {
//        TxRequiredCommandServerLocalHome cmdSrvHome = (TxRequiredCommandServerLocalHome) sl.getEJBLocalHome("TxRequiredCommandServer");
//        TxRequiredCommandServerLocal cmdSrv = cmdSrvHome.create();
//        cmdSrv.executeCommand(cmd);
//      }
//      else if(cmd instanceof TxNotSupported) {
//        TxNotSupportedCommandServerLocalHome cmdSrvHome = (TxNotSupportedCommandServerLocalHome) sl.getEJBLocalHome("TxNotSupportedCommandServer");
//        TxNotSupportedCommandServerLocal cmdSrv = cmdSrvHome.create();
//        cmdSrv.executeCommand(cmd);
//      }
//      else {
//        throw new CommandException("Cannot instanciate command server");
//      }
//      
System.out.println("CT: Inside commandTarget. about to diffrientate what instance");
context = JNDILookupClass.getInitialContext();
if (cmd instanceof TxRequired) {
System.out.println("CT: TxRequired");
TxRequiredCmdServerRemote cmdSrv = (TxRequiredCmdServerRemote)context.lookup(JNDILookupClass.getLookupName("TxRequiredCmdServerRemoteBean", TxRequiredCmdServerRemote.class.getName()));
cmdSrv.executeCommand(cmd);
}
else if(cmd instanceof TxNotSupported) {
System.out.println("CT: TxNotSupported");
System.out.println("CT: cmd: " + cmd.getClass());
TxNotSupportedCmdServerRemote cmdSrv = (TxNotSupportedCmdServerRemote)context.lookup(JNDILookupClass.getLookupName("TxNotSupportedCmdServerBean", TxNotSupportedCmdServerRemote.class.getName()));
cmdSrv.executeCommand(cmd);
}
else {
throw new CommandException("Cannot instanciate command server");
}
}
catch (CommandException ex) {
throw ex;
}
//    catch (CreateException ex) {
//      throw new CommandException(ex);
//    }
//new catch
catch(NamingException ex) {
throw new CommandException(ex);
}
catch (ServiceLocatorException ex) {
throw new CommandException(ex);
}
}
}

唷。。。 好的,现在这是EJBClient的重要部分。现在转到 Web.war
我只粘贴实际运行的部分,如果它返回一个空点

public class ActionIdentitetKonsultCommand implements Command {

private static Logger logger = Logger.getLogger(ActionIdentitetKonsultCommand.class);

public ActionIdentitetKonsultCommand() {
}
public String execute(RequestContext requestContext) throws CommandException {
GetPersonByPersnrEJBCommand personCmd;
logger.info("execute()");
try {
UserBean user = (UserBean) requestContext.getSession().getAttribute("user");
String kstnr = requestContext.getParameter("kstnr");
//Tilldela konsultuppgifter
personCmd = new GetPersonByPersnrEJBCommand();
personCmd.setPersnr(user.getPersnr());
System.out.println("AI: Before execute DTO " + personCmd.dto);
personCmd.execute();
System.out.println("AI: After execute DTO " + personCmd.dto);
logger.info("person hamtad med personnummer (EJB):");
logger.info(personCmd.getPerson().toString()); 

所以personCmd.getPerson((.tostring((是导致空指针的原因。 GetPersonByPersnrEJBCommand((:

public class GetPersonByPersnrEJBCommand extends TargetableCommand implements TxNotSupported {
public PersonDTO dto;
private long persnr;
public GetPersonByPersnrEJBCommand() {
}
public void setPersnr(long persnr) {
this.persnr = persnr;
}
public PersonDTO getPerson() {
return this.dto;
}
public void performExecute() throws CommandException {
try {
QueryPersonByPersnrCommand cmd = new QueryPersonByPersnrCommand();

cmd.setPersnr(persnr);
cmd.execute();
if(cmd.next()){
this.dto = new PersonDTO();
System.out.println("GP: inside PerformExecute DTO: " + dto);
dto.setPersnr(cmd.getPersnr());
dto.setEfternamn(cmd.getEfternamn());
dto.setFornamn(cmd.getFornamn());
dto.setEpostAdress(cmd.getEpostAdress());
dto.setKonsult((cmd.getKonsult() == 1));
dto.setAnsvarig((cmd.getAnsvarig() == 1));

System.out.println("GP: Inside Perform Execute DTO: " + dto);
}
}
catch (DataAccessCommandException ex) {
System.out.println("GetPersonByPersnrEJBCommand.performExecute misslyckades " + ex.getMessage());
throw new CommandException(ex);
}
}
}

就是这样;我不明白为什么它忘记了它。当我们在最后一个类中执行 sysouts 时,我们看到 DTO 和 CMD 中都有数据,但是一旦函数结束并且我们回到调用它的类中,数据就是空的。 我怀疑这与我的会话豆子有关,我缺少属性或其他东西。因为这段代码适用于旧 JBOSS 服务器中的旧 bean。希望有人可以帮助我,其他人也可以从中学习。

我设法解决了这个问题。因为该项目的范围是让它工作。这不是一个漂亮的解决方案,如果有更多的时间重写,这会更好。 依此类推,直到解决方案: 我们需要在bean,targetableCommand,CommandTarget和GetPersonByPersnrEJBCommand中进行更改

。目标命令 - 添加方法:

public TargetableCommand execute(TargetableCommand  cmd) throws CommandException
{    
return CommandTarget.executeCommand(cmd);
}

CommandTarget - 我们更改方法 executeCommand 以返回 TargetableCommand,并确保在 bean 完成后返回该 cmd。

public static TargetableCommand executeCommand(TargetableCommand cmd) throws CommandException {
Context context = null;
try {     
context = JNDILookupClass.getInitialContext();
if (cmd instanceof TxRequired) {
TxRequiredCmdServerRemote cmdSrv = (TxRequiredCmdServerRemote)context.lookup(JNDILookupClass.getLookupName("TxRequiredCmdServerRemoteBean", TxRequiredCmdServerRemote.class.getName()));
cmd = cmdSrv.executeCommand(cmd);
}
else if(cmd instanceof TxNotSupported) {
TxNotSupportedCmdServerRemote cmdSrv = (TxNotSupportedCmdServerRemote)context.lookup(JNDILookupClass.getLookupName("TxNotSupportedCmdServerBean", TxNotSupportedCmdServerRemote.class.getName()));
cmd = cmdSrv.executeCommand(cmd);
}
else {
throw new CommandException("Cannot instanciate command server");
}
}
catch (CommandException ex) {
throw ex;
}
catch(NamingException ex) {
throw new CommandException(ex);
}
}
return cmd;
}

bean - cange 方法 Execute 命令以返回 Targetable命令

public TargetableCommand executeCommand(TargetableCommand cmd) throws CommandException {
try {
cmd = cmd.performExecute();
}
catch (CommandException ex) {
throw ex;
}
return cmd;
}

最后,为了让这一切正常工作,我必须在需要执行执行的类中创建一个新方法,因此在 GetPersonByPersnrEJBCommand 类中,我创建了方法 wf13Layer((; 这只是一个额外的步骤:

public void wf13Layer() throws CommandException
{
GetPersonByPersnrEJBCommand tmp;
try{
tmp = (GetPersonByPersnrEJBCommand) execute(this);
dto = tmp.getPerson();
} catch (Exception ex) {
throw new CommandException(ex);
}
}

这就是我为使其工作所做的。 正如我所说,这不是一个漂亮的解决方案,但它有效。IT似乎是一种组合,一旦我们在项目之间交叉,范围就会消失。为了进一步获得它,我们需要像这样分层。我真的希望这在某些时候对某人有所帮助,因为那里有很多旧代码在运行。

亲切问候 非常累

最新更新