Java EE コンポーネント内からこれを使用して、メッセージ送信が、コンポーネントにより使用された JTA トランザクションの一部として実行されることを指定します。メッセージ送信が失敗した場合は、トランザクション全体がロールバックされ、メッセージが再送信されます。以下に、MDB 内からのこの例を示します。
@MessageDriven(name = "MDBMessageSendTxExample",
activationConfig =
{
@ActivationConfigProperty
(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty
(propertyName = "destination", propertyValue = "queue/testQueue")
})
@TransactionManagement(value= TransactionManagementType.CONTAINER)
@TransactionAttribute(value= TransactionAttributeType.REQUIRED)
public class MDBMessageSendTxExample implements MessageListener
{
@Resource(mappedName = "java:/JmsXA")
ConnectionFactory connectionFactory;
@Resource(mappedName = "queue/replyQueue")
Queue replyQueue;
public void onMessage(Message message)
{
Connection conn = null;
try
{
//Step 9. We know the client is sending a text message so we cast
TextMessage textMessage = (TextMessage)message;
//Step 10. get the text from the message.
String text = textMessage.getText();
System.out.println("message " + text);
conn = connectionFactory.createConnection();
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = sess.createProducer(replyQueue);
producer.send(sess.createTextMessage("this is a reply"));
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if(conn != null)
{
try
{
conn.close();
}
catch (JMSException e)
{
}
}
}
}
}
@MessageDriven(name = "MDBMessageSendTxExample",
activationConfig =
{
@ActivationConfigProperty
(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty
(propertyName = "destination", propertyValue = "queue/testQueue")
})
@TransactionManagement(value= TransactionManagementType.CONTAINER)
@TransactionAttribute(value= TransactionAttributeType.REQUIRED)
public class MDBMessageSendTxExample implements MessageListener
{
@Resource(mappedName = "java:/JmsXA")
ConnectionFactory connectionFactory;
@Resource(mappedName = "queue/replyQueue")
Queue replyQueue;
public void onMessage(Message message)
{
Connection conn = null;
try
{
//Step 9. We know the client is sending a text message so we cast
TextMessage textMessage = (TextMessage)message;
//Step 10. get the text from the message.
String text = textMessage.getText();
System.out.println("message " + text);
conn = connectionFactory.createConnection();
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = sess.createProducer(replyQueue);
producer.send(sess.createTextMessage("this is a reply"));
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if(conn != null)
{
try
{
conn.close();
}
catch (JMSException e)
{
}
}
}
}
}
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
また、EJB (セッション、エンティティ、および Message-Driven Bean を含む)、サーブレット (JSP を含む)、およびカスタム MBean からメッセージを送信するために JMS JCA アダプターを使用できます。