【发布时间】:2019-04-11 11:19:00
【问题描述】:
我正在尝试使用 java-ee。我想要实现的是下一个: 我编写了一个从不同来源收集投注价格的应用程序,我将收集的数据管理到一个自定义列表类(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 实例的字段值,但如果它不更改每个 EventArquitectura 实例中包含的 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 对象,它们始终为空:
Id of the event added into constructor: evento del contructor id
Modified Bet Id: null
Added EventoArquitectura id: newEventId
Added Bet Id: null
【问题讨论】:
标签: java jakarta-ee ejb cdi