为什么将其他 ejb 作为字段的 ejb 不更新作为字段的此 ejb 的值?
Why an ejb that has another ejb as a field doesn´t update the values of this ejb that is acting as a field?
我正在尝试 java-ee。接下来我要实现的目标是:
我编写了一个从不同来源收集赌注价格的应用程序,我将收集的数据管理到自定义列表 class(ListEventsArquitectura) 中,该列表管理存储在 EventoArquitectura(自定义 class) 中的事件数据。这个 EventoArquitectura class 也有其他自定义对象作为字段,比如 BetArquitectura。
我正在测试的是此应用程序作为 Ejb 的客户端工作,发送收集的数据以便稍后作为 Ejb 的 Rest web 服务可用。
现在我有一个 ejb 远程接口来修改包含在 ListEventsArquitectura 中的 EventoArquitectura 实例的字段,它可以工作,但是由于 EventoArquitectura 实例具有作为 BetArquitectura 的字段实例,我还创建了另一个 Ejb 远程接口来修改 BetArquitectura 的字段和这是我遇到问题的地方,因为包含在 EventoArquitectura 中的字段 BetArquitectura 的更新不会产生任何变化。
我留下了为测试和远程客户端创建的 classes 代码。
为了澄清,我没有使用 @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 对象,它们总是空的:
Id of the event added into constructor: evento del contructor id
Modified Bet Id: null
Added EventoArquitectura id: newEventId
Added Bet Id: null
我认为您的问题是您修改了仅存在于客户端的实例的 属性。当您通过 EJB 客户端代理调用方法时,return 是一个对象,您将获得一个自己的实例(在服务器端序列化并在客户端反序列化)。这是两个不同的对象,一个存在于客户端,另一个存在于服务器端。这是使用远程 ejb 时的常见误解。
尝试像这样在您的 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 修改它之后检索它。同样,您将获得自己的实例,它不是代理,正如我假设的那样。只有服务被代理,而不是对象及其方法 return.
我正在尝试 java-ee。接下来我要实现的目标是: 我编写了一个从不同来源收集赌注价格的应用程序,我将收集的数据管理到自定义列表 class(ListEventsArquitectura) 中,该列表管理存储在 EventoArquitectura(自定义 class) 中的事件数据。这个 EventoArquitectura class 也有其他自定义对象作为字段,比如 BetArquitectura。
我正在测试的是此应用程序作为 Ejb 的客户端工作,发送收集的数据以便稍后作为 Ejb 的 Rest web 服务可用。
现在我有一个 ejb 远程接口来修改包含在 ListEventsArquitectura 中的 EventoArquitectura 实例的字段,它可以工作,但是由于 EventoArquitectura 实例具有作为 BetArquitectura 的字段实例,我还创建了另一个 Ejb 远程接口来修改 BetArquitectura 的字段和这是我遇到问题的地方,因为包含在 EventoArquitectura 中的字段 BetArquitectura 的更新不会产生任何变化。
我留下了为测试和远程客户端创建的 classes 代码。
为了澄清,我没有使用 @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 对象,它们总是空的:
Id of the event added into constructor: evento del contructor id
Modified Bet Id: null
Added EventoArquitectura id: newEventId
Added Bet Id: null
我认为您的问题是您修改了仅存在于客户端的实例的 属性。当您通过 EJB 客户端代理调用方法时,return 是一个对象,您将获得一个自己的实例(在服务器端序列化并在客户端反序列化)。这是两个不同的对象,一个存在于客户端,另一个存在于服务器端。这是使用远程 ejb 时的常见误解。
尝试像这样在您的 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 修改它之后检索它。同样,您将获得自己的实例,它不是代理,正如我假设的那样。只有服务被代理,而不是对象及其方法 return.