2012-11-28 41 views
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(); 
} 

} 

} 

感谢

回答

0

你使用Spring JMS模板试过吗?它为JMS提供了一个抽象层,当你的实现改变时它可能会帮助你。