Skip to content

Commit

Permalink
Merge pull request #8652 from jmesnil/MDB20TopicTestCase_intermittent…
Browse files Browse the repository at this point in the history
…_failures

Rewrite MDB20 Test case to fix intermittent failures
  • Loading branch information
stuartwdouglas committed Jun 3, 2016
2 parents 7a17cfb + 4553bdf commit ed944ad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,19 @@
*/
package org.jboss.as.test.integration.ejb.mdb.ejb2x;

import org.jboss.logging.Logger;

import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSContext;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.jboss.logging.Logger;

/**
* A replying EJB 2.x MDB.
Expand All @@ -46,43 +44,30 @@ public class EJB2xMDB implements MessageDrivenBean, MessageListener {

private static final Logger logger = Logger.getLogger(EJB2xMDB.class);

private MessageDrivenContext ctx = null;
private QueueConnection connection;
private QueueSession session;
private MessageDrivenContext mdbContext;

public EJB2xMDB() {
}
private ConnectionFactory cf;

@Override
public void setMessageDrivenContext(MessageDrivenContext ctx) {
this.ctx = ctx;
public EJB2xMDB() {
}

public void ejbCreate() {
InitialContext iniCtx = null;
try {
final InitialContext iniCtx = new InitialContext();
final QueueConnectionFactory factory = (QueueConnectionFactory) iniCtx.lookup("java:/ConnectionFactory");
connection = factory.createQueueConnection();
session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
connection.start();
} catch (Exception e) {
throw new EJBException("Failed to init EJB2xMDB", e);
iniCtx = new InitialContext();
cf = (ConnectionFactory) iniCtx.lookup("java:/ConnectionFactory");
} catch (NamingException e) {
throw new EJBException(e);
}
}

@Override
public void ejbRemove() {
ctx = null;
try {
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
} catch (JMSException e) {
e.printStackTrace();
}
public void setMessageDrivenContext(MessageDrivenContext ctx) throws EJBException {
this.mdbContext = ctx;
}

@Override
public void ejbRemove() throws EJBException {
}

@Override
Expand All @@ -92,21 +77,27 @@ public void onMessage(Message message) {
if (message.getStringProperty("MessageFormat") != null)
logger.info("MessageFormat property = " + message.getStringProperty("MessageFormat"));

Queue destination = (Queue) message.getJMSReplyTo();
if (destination == null) {
Destination replyTo = message.getJMSReplyTo();
if (replyTo == null) {
try {
destination = (Queue) this.ctx.lookup("jms/replyQueue");
} catch (Throwable optional) {}
System.out.println("mdbContext = " + mdbContext);
replyTo = (Destination) mdbContext.lookup("jms/replyQueue");
} catch (Throwable e) {
logger.warn(e);
}
} else {
logger.info("Using replyTo from message JMSReplyTo: " + replyTo);
}
if (destination != null) {
logger.info("replying to " + destination);
final TextMessage tm = (TextMessage) message;
final String text = tm.getText() + "processed by: " + hashCode();
final QueueSender sender = session.createSender(destination);
final TextMessage reply = session.createTextMessage(text);
reply.setJMSCorrelationID(message.getJMSMessageID());
sender.send(reply);
sender.close();
if (replyTo == null) {
throw new EJBException("no replyTo Destination");
}

final TextMessage tm = (TextMessage) message;
final String reply = tm.getText() + "processed by: " + hashCode();
try (JMSContext context = cf.createContext()) {
context.createProducer()
.setJMSCorrelationID(message.getJMSMessageID())
.send(replyTo, reply);
}
} catch (Exception e) {
logger.error(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static class JmsQueueSetup implements ServerSetupTask {

@Override
public void setup(ManagementClient managementClient, String containerId) throws Exception {
jmsAdminOperations = JMSOperationsProvider.getInstance(managementClient);
jmsAdminOperations = JMSOperationsProvider.getInstance(managementClient.getControllerClient());
jmsAdminOperations.createJmsTopic("ejb2x/topic", "java:jboss/ejb2x/topic");
jmsAdminOperations.createJmsQueue("ejb2x/replyQueueA", "java:jboss/ejb2x/replyQueueA");
jmsAdminOperations.createJmsQueue("ejb2x/replyQueueB", "java:jboss/ejb2x/replyQueueB");
Expand Down

0 comments on commit ed944ad

Please sign in to comment.