2

GlassfishサーバーでJMSをテストしているので、Glassfishサーバーのキューで簡単なテキストメッセージを送信します。ActiveMQを試してみましたが、うまくいきましたが、構成jndi.propertiesファイルに何を入れることができ、glassfishサーバーにどのjarが必要かを理解できません。これを実装するためのアイデアを教えてください。

前もって感謝します

4

1 に答える 1

2

Since you're using Glassfish, the easiest way is to write simple application (EJB) that will perform the task. You have to define in GF:

  • ConnectionFactory (Resources -> JMS Resources -> Connection Factory), let's give it JNDI name jms/ConnectionFactory
  • Message queue (Resources -> JMS Resources -> Destination Resources), let's give it JNDI name jms/myQueue

Next step is to use these in some EJB that you need to write. It's not hard: firstly, you have to inject:

@Resource(mappedName="jms/ConnectionFactory")
private ConnectionFactory cf;

@Resource(mappedName="jms/myQueue")
private Queue messageQueue;

and then use it like this:

..
    javax.jms.Connection conn = null;
    javax.jms.Session s = null;
    javax.jms.MessageProducer mp = null
    try {
        conn = cf.createConnection();
        s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        mp = s.createProducer(messageQueue);
        javax.jms.TextMessage msg = s.createTextMessage();
        msg.setStringProperty("your-key", "your-value");
        msg.setText("Your text message");
        mp.send(msg);        
    }
    catch(JMSException ex) {
        // exception handling
    }
    finally {
        try {
            // close Connection, Session and MessageProducer
        } catch (JMSException ex) {
                //exception handling
        }
    }

Regarding configuration, you don't need any external JAR, everything that is needed is shipped. If you don't want to write EJB, but regular Java (standalone) application, then you'll have to include jms.jar and imq.jar.

于 2012-11-22T08:39:37.507 に答える