0

任意の jms サーバーでメッセージを送信するための汎用コードを記述したいと考えています。そのため、jndi.propertiesファイルがあれば、サーバー構成をこのファイルに配置でき、コードを介してこのファイルにアクセスできますが、「ActiveMQサーバー」に対してのみこれを行うことができます。現在、グラスフィッシュサーバーやjbossサーバーなどの他のサーバーでメッセージを送信する際に問題に直面しています。誰かが私がこの仕事をするのを手伝ってくれませんか。

これが私のコードです:

public class Producer 
{
   public Producer() throws JMSException, NamingException,IOException 
  {
   InputStream is = getClass().getResourceAsStream("my.jndi.properties");
   Properties jndiParamaters = new Properties();
   jndiParamaters.load(is);  
   Context jndi = new InitialContext(jndiParamaters); 
   ConnectionFactory conFactory = (ConnectionFactory) jndi.lookup("connectionFactory");
   Connection connection; 
   connection = conFactory.createConnection();
try 
{
    connection.start();   
   Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);  
    Destination destination = (Destination) jndi.lookup("Myqueue"); 
    MessageProducer producer = session.createProducer(destination); 
    TextMessage message = session.createTextMessage("Hello World!"); 
    producer.send(message);
    System.out.println("Sent message '" + message.getText() + "'");
} 
finally 
{
   connection.close();
}
}
public static void main(String[] args) throws JMSException 
{   
try
{
   BasicConfigurator.configure();
    new Producer();
} 
catch (Exception e) 
{
  e.printStackTrace();
}

}

}

ありがとう

4

1 に答える 1

0

Spring JMS テンプレートを使用してみましたか? http://static.springsource.org/spring/docs/2.0.x/reference/jms.html JMS に抽象化レイヤーを提供し、実装が変更されたときに役立つ可能性があります。

于 2012-11-28T12:33:10.680 に答える