2012-09-21 49 views
2

我正在尝试与C++和Java之间的JMS通信开发应用程序。将C++连接到ActiveMQ代理

我有一个“服务器”在Java的经纪人,我想瞬移一个C++出版商/听者

如何我做到这一点?

我的班IM Java是:

“SERVER”:

public class Queue { 

private static ActiveMQConnectionFactory connectionFactory; 
private static Destination destination; 
private static boolean transacted = false; 
private static Session session; 
private static Connection connection; 

public static void main(String[] args) throws Exception { 
    BrokerService broker = new BrokerService(); 
    broker.setUseJmx(true); 
    broker.addConnector("tcp://localhost:61616"); 
    broker.start(); 
    Producer p=new Producer(); 
    Consumer c= new Consumer(); 
    connectionFactory = new ActiveMQConnectionFactory(
      ActiveMQConnection.DEFAULT_USER, 
      ActiveMQConnection.DEFAULT_PASSWORD, 
      ActiveMQConnection.DEFAULT_BROKER_URL); 
    connection = connectionFactory.createConnection(); 
    connection.start(); 
    session = connection 
      .createSession(transacted, Session.AUTO_ACKNOWLEDGE); 
    destination = session.createQueue("queue"); 
    c.createConsumerAndReceiveAMessage(connection, connectionFactory,session,destination); 
    p.createProducerAndSendAMessage(destination,session); 
    broker.stop(); 
} 

生产者

public class Producer { 
void createProducerAndSendAMessage(Destination destination, 
     Session session) throws JMSException { 

    MessageProducer producer = session.createProducer(destination); 
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 
    Scanner sc=new Scanner(System.in); 
    String msg; 
    while(!(msg=sc.nextLine()).equals("exit")){ 
     TextMessage message = session.createTextMessage(msg); 
     System.out.println("Sending message " + message.getText()); 
     producer.send(message); 
    } 
} 

消费者:

public class Consumer { 
public void createConsumerAndReceiveAMessage(Connection connection, 
     ActiveMQConnectionFactory connectionFactory, Session session, 
     Destination destination) throws JMSException, InterruptedException { 

    connection = connectionFactory.createConnection(); 
    connection.start(); 
    MessageConsumer consumer = session.createConsumer(destination); 
    MyConsumer myConsumer = new MyConsumer(); 
    connection.setExceptionListener(myConsumer); 
    consumer.setMessageListener(myConsumer); 
} 
private static class MyConsumer implements MessageListener, 
     ExceptionListener { 
    synchronized public void onException(JMSException ex) { 
     System.out.println("JMS Exception occured. Shutting down client."); 
     System.exit(1); 
    } 
    public void onMessage(Message message) { 
     if (message instanceof TextMessage) { 
      TextMessage textMessage = (TextMessage) message; 
      try { 
       System.out.println("Received message " 
         + textMessage.getText()); 
      } catch (JMSException ex) { 
       System.out.println("Error reading message " + ex); 
      } 
     } else { 
      System.out.println("Received " + message); 
     } 
    } 
} 

问候

+2

[你有什么尝试?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) –

回答

1

你有没有看ActiveMQ-CPP?这是ActiveMQ C++客户端,在项目的主页面有文档,示例和教程。

+0

我已经看过了,但我不能编译库下载。 –

+0

好的......你没有提到你试过的东西。你发布的java代码是不相关的,那么发布你所拥有的C++代码和编译时出现的错误怎么样? – jkysam

+0

当我编译时,C++中的主文件不能解决依赖性问题,比如''但是当我用make文件编译activeMQ-cpp的默认示例时,他正在编译完美。 –