問題を正しく理解MyMDB
し、WebLogicのトピックをリッスンし、JBossによって提供され、デプロイされた構成ファイルで定義され、JNDI名で識別される追加のJMS宛先を使用したい場合(デフォルトでdeploy/jms/jms-ds.xml
は、JMSの構成のみが含まれます)プロバイダーおよび接続ファクトリ-データソースなし)。
最も簡単な方法は、コンテナにJMDI名を介してJMS宛先と接続ファクトリを注入させることです(JBossでは、JMS宛先はxxx-service.xmlファイルをデプロイすることによって設定されます)。起動時に接続を初期化し、MDBが解放されたらすぐにクリーンアップを実行できます。
次の例は、インジェクション(@Resource
)とリソース管理(@PostConstruct
および@PreDestroy
)を示しています。JMS接続と宛先はuseJmsDestination(String)
、テキストメッセージの送信に使用されます。
public class MyMDB implements MessageListener {
@Resource(mappedName = "queue/YourQueueName") // can be topic too
private Queue targetDestination;
@Resource(mappedName = "QueueConnectionFactory") // or ConnectionFactory
private QueueConnectionFactory factory;
private Connection conn;
public void onMessage(Message m) {
// parse message and do what you need to do
...
// do something with the message and the JBoss JMS destination
useJmsDestination(messageString);
}
private void useJmsDestination(String text) {
Session session = null;
MessageProducer producer = null;
try {
session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(targetDestination);
TextMessage msg = session.createTextMessage(text);
producer.send(msg);
} catch (JMSException e) {
throw new RuntimeException(e);
} finally {
try {
if (producer != null) {
producer.close();
}
if (session != null) {
session.close();
}
} catch (JMSException e) {
// handle error, should be non-fatal, as the message is already sent.
}
}
}
@PostConstruct
void init() {
initConnection();
// other initialization logic
...
}
@PreDestroy
void cleanUp() {
closeConnection();
// other cleanup logic
...
}
private void initConnection() {
try {
conn = factory.createConnection();
} catch (JMSException e) {
throw new RuntimeException("Could not initialize connection", e);
}
}
private void closeConnection() {
try {
conn.close();
} catch (JMSException e) {
// handle error, should be non-fatal, as the connection is being closed
}
}
}
これがお役に立てば幸いです。