gpt4 book ai didi

java - 有状态 session bean 的消息驱动 bean 没有响应

转载 作者:行者123 更新时间:2023-12-01 12:49:29 24 4
gpt4 key购买 nike

我正在尝试使工作成为消息驱动的 bean,但没有成功。也许你可以帮助我。

这是我的 MDB 代码,非常简单

@MessageDriven(mappedName = "jms/bookingProcessorQueueReceiver", 
activationConfig = {@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class BookingProcessorBean implements MessageListener
{
@PersistenceContext
private EntityManager em;

public BookingProcessorBean()
{
}

@Override
public void onMessage(Message message)
{
Reservation reserv = null;

if(message instanceof Reservation)
{
try
{

System.out.println("Order processing...");

reserv = (Reservation) message; // riceve l'utente

UserCredential user = reserv.getUsername(); //estrae l'utente

user.getReservationsList().add(reserv);

storeInDb(reserv, user);

}
catch(Exception e)
{
System.err.println("An error occured during the processing of the order" + e.toString());
}
}
}

private void storeInDb(Reservation r, UserCredential u)
{
em.persist(r);
em.merge(u);
}
}

这是调用它的有状态 session bean

@Stateful
public class BookingBean implements BookingBeanInterface
{
@Resource(mappedName = "jms/bookingProcessorQueueReceiver")
private Queue bookingProcessorQueueReceiver;
@Resource(mappedName = "jms/reservationProcessorQueueFactory")
private ConnectionFactory reservationProcessorQueueFactory;

private Connection connection = null;
private Session session = null;

private MessageProducer messageProducer;

private boolean SUCCESS;

@Override
public boolean purchase(Route r, UserCredential u, int passengers, int luggages, double price)
{
Reservation reserv = setReservation(r, u, passengers, luggages, (float) price);

try
{
sendMessageToBookingProcessorBean(reserv); // invia il messaggio contenente la prenotazione

SUCCESS = true;

}
catch(JMSException jmse)
{
System.err.println("An error occured " + jmse.toString());
return SUCCESS = false;
}
finally
{
try
{
if (session != null)
{
session.close();
}

if (connection != null)
{
connection.close();
}
}
catch(JMSException jmse)
{
Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", jmse);
}
}

return SUCCESS;
}

private void sendMessageToBookingProcessorBean(Object messageData) throws JMSException
{
connection = reservationProcessorQueueFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

messageProducer = session.createProducer(bookingProcessorQueueReceiver);

messageProducer.send(createMessageForBookingProcessorBean(session, messageData));
}

private Message createMessageForBookingProcessorBean(Session session, Object messageData) throws JMSException
{

connection.start();

ObjectMessage msg = session.createObjectMessage((Serializable) messageData);


return msg;
}
}

我不知道我是否忘记了什么。请检查代码并告诉我是否一切正常...奇怪的事实是当我发送消息时我没有收到来自 MDB 的任何响应。我放入其中的任何代码都不会被执行。我的意思是,例如我简单的 system.out.println 没有被执行。

最佳答案

在你的比较中

if (message instanceof Reservation)

您的消息可能属于技术类型(例如TextMessage,或者在您的情况下是ObjectMessage)。

你需要类似的东西

if (message instanceof ObjectMessage) {
Object content = message.getObject();
if (content instanceof Reservation) {
// Cast and proceed

关于java - 有状态 session bean 的消息驱动 bean 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24346148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com