【问题标题】:jms sending message on any serverjms 在任何服务器上发送消息
【发布时间】:2012-11-28 20:33:10
【问题描述】:

我想编写用于在任何 jms 服务器上发送消息的通用代码。所以为此我想如果我有 jndi.properties 文件,那么我们可以将服务器配置放在这个文件中,我们可以通过代码访问这个文件,但我只能为“ActiveMQ 服务器”执行此操作。现在我面临在任何其他服务器(如 glassfish 服务器或 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();
}

}

}

谢谢

【问题讨论】:

    标签: jms sendmessage


    【解决方案1】:

    您是否尝试过使用 Spring JMS 模板? http://static.springsource.org/spring/docs/2.0.x/reference/jms.html 它为 JMS 提供了一个抽象层,并且可能会在您的实现发生变化时为您提供帮助。

    【讨论】:

      猜你喜欢
      • 2012-11-10
      • 2014-02-02
      • 1970-01-01
      • 2012-07-08
      • 2012-11-18
      • 1970-01-01
      • 2013-02-02
      • 2013-10-27
      • 2013-10-06
      相关资源
      最近更新 更多