如何从同一个GlassFish服务器上的其他应用程序注入EJB? [英] How can I inject an EJB from an other application on the same GlassFish Server?
问题描述
远程接口(bicyle app) :
@Remote
public interface FahrradRemote {
public List&Fahrrad> findAvaible(Date start,Date end);
public void addAuftrag(Fahrrad fahrrad,Date startDate,Date endDate);
public boolean login(String name,String passwort);
}
自行车EJB我想打电话:
@Stateless(name =FahrradService,mappedName =FahrradService)
public class FahrradService实现javax.ejb.SessionBean,FahrradRemote,Serializable {
@PersistenceContext
private EntityManager em;
Kunde客户;
...
}
火车应用程序的开始:
@Named
@SessionScoped
public class BahnPM实现Serializable {
private String startStation ;
private String zielStation;
private String loginName;
private String loginPasswort;
private String customerName;
private String startDateString;
private Date startDate;
私人列表< Fahrt>可用的;
私人名单站;
@EJB
私人BahnService服务;
@EJB(mappedName =FahrradService)
private static FahrradRemote fahrradService;
public BahnPM(){
}
...
}
错误我得到的是一个类没有找到的异常,我可以理解,因为它是一个不同的应用程序,我的火车应用程序不知道FahrradRemote,但是如何注入EJB ?
在自行车应用程序中,您必须:
- 从
界面中删除
@Remote
注释FahrradRemote
<将
@Remote
注释添加到您的 FahrradService
EJB
您可以关注此片段:
@Remote(FahrradRemote.class)
@Stateless
public class FahrradService实现FahrradRemote,Serializable {
.... //您的代码
}
(如果您使用的是EJB 3.X,则不需要EJB显式实现SessionBean接口) p>
在你的火车应用程序中:
@EJB(name =FahrradService)
private FahrradRemote fahrradService;
(使用名称
属性而不是 mappedName
;在无状态EJB中不能有静态属性)
最后你必须告诉容器在哪里查找EJB实现:在 glassfish-ejb-jar
标签内创建描述符 glassfish-ejb-jar.xml
这样:
< enterprise-beans>
< ejb>
< ejb-name> BahnPM< / ejb-name>
< ejb-ref>
< ejb-ref-name> FahrradService< / ejb-ref-name>
< jndi-name> java:global / MyRemoteBeanModule / MyRemoteBean< / jndi-name>
< / ejb-ref>
< / ejb>
< / enterprise-beans>
您的远程EJB的便携式JNDI名称(我所称的 java: global / MyRemoteBeanModule / MyRemoteBean
)在部署自行车应用程序时,可以在GlassFish日志中使用。
I have two applications running on my local glassfish server. One to rent bicylces and one to buy train tickets. I now wanted to call a remote ejb from the train application to allow the rental of bicycles for the chosen time but I'm not really sure how to do it. I already tried a few different approaches. Both applications are in different packages and the train application has to know the bicycle remote interface but I don't know how to accomplish that.
The remote interface (bicyle app):
@Remote
public interface FahrradRemote {
public List<Fahrrad> findAvaible(Date start, Date end);
public void addAuftrag(Fahrrad fahrrad, Date startDate, Date endDate);
public boolean login(String name, String passwort);
}
Bicycle EJB I want to call:
@Stateless(name="FahrradService",mappedName="FahrradService")
public class FahrradService implements javax.ejb.SessionBean, FahrradRemote, Serializable {
@PersistenceContext
private EntityManager em;
Kunde customer;
...
}
Beginning of the train app:
@Named
@SessionScoped
public class BahnPM implements Serializable {
private String startStation;
private String zielStation;
private String loginName;
private String loginPasswort;
private String customerName;
private String startDateString;
private Date startDate;
private List<Fahrt> available;
private List stations;
@EJB
private BahnService service;
@EJB(mappedName="FahrradService")
private static FahrradRemote fahrradService;
public BahnPM() {
}
...
}
Error I get is a class not found exception, which i can understand because it's a different application and my train app doesn't know "FahrradRemote" but how can I inject that EJB?
In your bicycle app you have to:
- remove the
@Remote
annotation from yourinterface FahrradRemote
- add the
@Remote
annotation to yourFahrradService
EJB
You can follow this snippet:
@Remote(FahrradRemote.class)
@Stateless
public class FahrradService implements FahrradRemote, Serializable {
.... // your code
}
(if your are using EJB 3.X, there is no need for an EJB to explicitly implement the SessionBean interface)
In your train app:
@EJB(name="FahrradService")
private FahrradRemote fahrradService;
(use name
attribute instead of mappedName
; and you cannot have static properties in a stateless EJB)
Finally you have to tell the container where to lookup for the EJB implementation: create the descriptor glassfish-ejb-jar.xml
and, inside glassfish-ejb-jar
tags, put this:
<enterprise-beans>
<ejb>
<ejb-name>BahnPM</ejb-name>
<ejb-ref>
<ejb-ref-name>FahrradService</ejb-ref-name>
<jndi-name>java:global/MyRemoteBeanModule/MyRemoteBean</jndi-name>
</ejb-ref>
</ejb>
</enterprise-beans>
The portable JNDI name for your remote EJB (what I have called java:global/MyRemoteBeanModule/MyRemoteBean
) is available in GlassFish logs when you deploy the bicycle application.
这篇关于如何从同一个GlassFish服务器上的其他应用程序注入EJB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!