2

I'm trying develop aplication with comunication with JMS between C++ and Java.

I have a "server" with a broker in Java and i would like conect a c++ publisher/listner

How to i do this?

My classes im Java are:

"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();  
}   

PRODUCER

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);
    }
}

CONSUMER:

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);
        }
    }
}

Regards

Alexandre Pinheiro
  • 370
  • 2
  • 6
  • 16

1 Answers1

1

Have you looked at ActiveMQ-CPP? This is the ActiveMQ C++ client, in the main page for the project there is documentation, examples and tutorials.

jkysam
  • 5,533
  • 1
  • 21
  • 16
  • i already took a look, but I can not compile the libraries that are downloadable. – Alexandre Pinheiro Sep 21 '12 at 13:36
  • ok... you didn't mention anything about what you tried. The java code you posted is irrelevant, how about posting the c++ code you have and errors you get when compiling? – jkysam Sep 21 '12 at 14:48
  • When i complile, the main file in C++, don't resolve the dependencies, like `` for example.. But when i compile the default examples of activeMQ-cpp with make file he's compiles perfectly. – Alexandre Pinheiro Sep 24 '12 at 13:32