非同期JMSはどのように機能しますか?JavaでのJMSの仕組みは?
public class JmsAdapter implements MessageListener, ExceptionListener
{
private ConnectionFactory connFactory = null;
private Connection conn = null;
private Session session = null;
public void receiveMessages()
{
try
{
this.session = this.conn.createSession(true, Session.SESSION_TRANSACTED);
this.conn.setExceptionListener(this);
Destination destination = this.session.createQueue("SOME_QUEUE_NAME");
this.consumer = this.session.createConsumer(destination);
this.consumer.setMessageListener(this);
this.conn.start();
}
catch (JMSException e)
{
//Handle JMS Exceptions Here
}
}
@Override
public void onMessage(Message message)
{
try
{
//Do Message Processing Here
//Message sucessfully processed... Go ahead and commit the transaction.
this.session.commit();
}
catch(SomeApplicationException e)
{
//Message processing failed.
//Do whatever you need to do here for the exception.
//NOTE: You may need to check the redelivery count of this message first
//and just commit it after it fails a predefined number of times (Make sure you
//store it somewhere if you don't want to lose it). This way you're process isn't
//handling the same failed message over and over again.
this.session.rollback()
}
}
}
しかし、私は、Java & JMSへの新たなんだ。私は、サンプルコードの下にきました。私はおそらくonMessageメソッドでメッセージを消費します。しかし、私は正確にどのように動作するのか分かりません。
JmsAdapterクラスにmainメソッドを追加する必要がありますか? mainメソッドを追加した後、jarを作成して&する必要がありますか?jarを "java -jar abc.jar"として実行しますか?
ご迷惑をおかけして申し訳ありません。
私が知りたいことは、mainメソッドを追加すると、mainでreceiveMessages()を呼び出すだけでいいのですか?そして走った後、リスナーは走り続けますか?メッセージがある場合は、onMessageメソッドで自動的に取得されますか?
また、リスナーが引き続きリスニングしている場合は、CPUを使用しませんか?スレッドの場合、スレッド&をスリープ状態にすると、CPU使用率はゼロになります。リスナーの場合はどのように動作しますか?
注:私はTomcatサーバーしか持っていません&私はどんなjmsサーバーも使用しません。リスナーにJBossなどの特定のjmsサーバーが必要なのかどうかはわかりません。しかし、いずれにしても、私は風穴以外何も持っていないと仮定してください。 ありがとう!
私はあなたの質問がJMSと何をしているかはわかりません。あなたは "Javaでプログラムを実行する方法"を求めているようです。 – skaffman
メインメソッドを作成せずに、サーバーにデプロイすると、クラスが監視しているキューに送信されたメッセージはすべてonMessage()メソッドによって処理されます。 onMessage()には、メッセージがキューに到着したときに実行するロジックが含まれます。 – Logan
JMS実装には何を使用していますか? –