我正在制作我的第一个java-ee方法。我接下来想要实现的是:我编写了一个应用程序,它收集不同来源的赌注价格,我将收集的数据管理到一个自定义List类(ListEventsArquitectura),管理存储在EventoArquitectura(自定义类)中的事件数据。这个EventoArquitectura类还有其他自定义对象,如BetArquitectura。
我正在测试的是,该应用程序作为Ejb的客户端发送收集的数据,以便稍后将其作为Ejb的Rest Web服务提供。
现在我有一个ejb远程接口来修改ListEventsArquitectura中包含的EventoArquitectura实例的字段,它可以工作,但是当EventoArquitectura实例作为BetArquitectura的字段实例时,我还创建了另一个Ejb远程接口来修改BetArquitectura的字段,这里是我的地方问题是因为包含在EventoArquitectura中的BetArquitectura字段的更新不会产生任何变化。
我留下了为测试和远程客户端创建的类的代码。
为了澄清,我没有使用@Inject,因为它产生了部署到glassfish的错误,因此我将注释更改为@Ejb。
@Stateful
public class ListEventsArquitectura implements ListEventsArquitecturaService{
List<EventoArquitectura> listaDeEventos;
@Ejb
private EventoArquitectura eventoService;
public ListEventsArquitectura() {
this.listaDeEventos = new ArrayList<>();
List<BetArquitectura> betsList = new ArrayList<>();
//betsList.add(new BetArquitectura("betDelConstructor", "0"));
this.listaDeEventos.add(new EventoArquitectura("evento del contructor id", "betIdDelConstructor"));
}
public List<EventoArquitectura> getListaDeEventos() {
return listaDeEventos;
}
@Override
public void updateListEvent(EventoArquitectura eventoActualizado){
for(EventoArquitectura evento : this.listaDeEventos){
if(evento.equals(eventoActualizado)){
this.eventoService = evento;
eventoService.updateEvent(eventoActualizado);
return;
}
}
}
@Override
public EventoArquitectura getEventFromList(int index) {
return this.listaDeEventos.get(index);
}
@Override
public void addEvent(EventoArquitectura evento) {
this.listaDeEventos.add(evento);
}
}
public interface ListEventsArquitecturaService {
public void updateListEvent(EventoArquitectura updatedEvent);
public EventoArquitectura getEventFromList(int index);
public void addEvent(EventoArquitectura evento);
}
@Stateful
public class EventoArquitectura implements Serializable,EventoArquitecturaService {
String eventId;
// List<BetArquitectura> betsList;
String betId;
public EventoArquitectura() {
}
public EventoArquitectura(String eventId, String betId) {
this.eventId = eventId;
//this.betsList = betsList;
}
public String getEventId() {
return eventId;
}
public void setEventId(String eventId) {
this.eventId = eventId;
}
/* public List<BetArquitectura> getBetsList() {
return betsList;
}
public void setBetsList(List<BetArquitectura> betsList) {
this.betsList = betsList;
}
*/
public String getBetId() {
return betId;
}
public void setBetId(String betId) {
this.betId = betId;
}
@Override
public void updateEvent(EventoArquitectura updatedEvent){
if(!(updatedEvent.equals(this))){
this.eventId = updatedEvent.eventId;
}
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof EventoArquitectura)){
return false;
}
EventoArquitectura evento = (EventoArquitectura)obj;
return evento.eventId.equals(this.eventId);
}
@Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + Objects.hashCode(this.eventId);
return hash;
}
}
@Remote
public interface EventoArquitecturaService {
public void updateEvent(EventoArquitectura updatedEvent);
public String getBetId();
public void setBetId(String betId);
}
public class BetArquitectura implements Serializable{
String marketId;
String valorBet;
public BetArquitectura(String marketId, String valorBet) {
this.marketId = marketId;
this.valorBet = valorBet;
}
public BetArquitectura() {
}
public String getMarketId() {
return marketId;
}
public void setMarketId(String marketId) {
this.marketId = marketId;
}
public String getValorBet() {
return valorBet;
}
public void setValorBet(String valorBet) {
this.valorBet = valorBet;
}
private void updateValor(String valorBet){
this.valorBet = valorBet;
}
public void updateBet(BetArquitectura betActualizada){
if(this.equals(betActualizada)){
this.updateValor(betActualizada.getValorBet());
}
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof BetArquitectura)){
return false;
}
BetArquitectura bet = (BetArquitectura)obj;
return bet.marketId.equals(this.marketId);
}
@Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + Objects.hashCode(this.marketId);
return hash;
}
}
在这里我离开我的远程客户端,它可以更改ListEventsArquitectura中包含的EventoArquitectura实例的字段值,但是如果它没有对每个EventoArquitectura实例中包含的BetArquitectura对象进行更改。
public class ClienteArquitecturaTest {
public static void main(String[] args){
try {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
// optional. Default localhost. Aquise cambia la IP del servidor donde esta Glassfishprops.setProperty("org.omg.CORBA.ORBInitialHost", "127.0.0.1");
// optional. Puerto por Default 3700. Solo se necesita cambiar si el puerto no es 3700.
//props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
Context jndi;
jndi = new InitialContext(props);
ListEventsArquitecturaService listEventsService = (ListEventsArquitecturaService) jndi.lookup("java:global/ArquitecturaEJBTest/ListEventsArquitectura!com.mycompany.ejb.interfaces.ListEventsArquitecturaService");
System.out.println("Id of the event added into constructor: " + listEventsService.getEventFromList(0).getEventId());
EventoArquitecturaService eventoParaModificar = listEventsService.getEventFromList(0);
eventoParaModificar.setBetId("betIdModified");
listEventsService.addEvent(new EventoArquitectura("newEventId", "newBetId"));
System.out.println("Modified Bet Id: " + listEventsService.getEventFromList(0).getBetId());
System.out.println("Added EventoArquitectura id: " + listEventsService.getEventFromList(1).getEventId());
System.out.println("Added Bet Id: " + listEventsService.getEventFromList(1).getBetId());
} catch (NamingException ex) {
Logger.getLogger(ClienteArquitecturaTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我用这个客户端获得的输出显示我没有实现修改BetArquitectura对象,它们总是为null:
Id of the event added into constructor: evento del contructor id
Modified Bet Id: null
Added EventoArquitectura id: newEventId
Added Bet Id: null
我认为你的问题是你修改了一个只存在于客户端的实例的属性。当您通过返回对象的EJB客户端代理调用方法时,您将获得一个自己的实例(在服务器端序列化并在客户端反序列化)。这是两个不同的对象,一个存在于客户端,另一个存在于服务器端。这是在使用远程ejbs时常见的误解。
尝试像这样在EJB上实现一个方法,它修改了服务器端的目标对象属性。
setIdAt(Integer idx, String id)
然后在客户端实现
// will pass parameters to server side EJB, which modifies the object property
service.setIdAt(0, "betIdModified");
// Get server side modified instance and its id
service.getElementFromList(0).getBetId();
通过该示例,您将参数传递给服务器端EJB,并且服务器端的对象得到修改,您可以在服务器端EJB修改它之后检索该对象。再次,你得到自己的实例,这不是代理,因为我假设你期望它。只代理服务而不代理其方法返回的对象。