2012-09-21 72 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的默認示例時,他正在編譯完美。 –