任意の 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();
}
}
}
ありがとう