0
我想編寫在任何jms服務器上發送消息的通用代碼。所以爲此,我想如果我有jndi.properties文件,那麼我們可以將服務器配置放在這個文件中,我們可以通過代碼訪問這個文件,但是我只能爲'ActiveMQ Server'做到這一點。現在我面臨的問題是在Glassfish服務器或jboss服務器等任何其他服務器上發送消息。有人可以幫助我完成這項任務嗎?jms在任何服務器上發送消息
這裏是我的代碼:
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();
}
}
}
感謝