Exemplo n.º 1
0
int main(int argc, char* argv[]) {

    activemq::library::ActiveMQCPP::initializeLibrary();

    std::cout << "=====================================================\n";
    std::cout << "Starting the Publisher example:" << std::endl;
    std::cout << "-----------------------------------------------------\n";

    std::string user = getEnv("ACTIVEMQ_USER", "admin");
    std::string password = getEnv("ACTIVEMQ_PASSWORD", "password");
    std::string host = getEnv("ACTIVEMQ_HOST", "localhost");
    int port = Integer::parseInt(getEnv("ACTIVEMQ_PORT", "61613"));
    std::string destination = getArg(argv, argc, 1, "event");

    int messages = 10000;
    int size = 256;

    std::string DATA = "abcdefghijklmnopqrstuvwxyz";
    std::string body = "";
    for( int i=0; i < size; i ++) {
        body += DATA.at(i%DATA.length());
    }

    {
        ActiveMQConnectionFactory factory;
        factory.setBrokerURI(std::string("tcp://") + host + ":" + Integer::toString(port) + "?wireFormat=stomp");

        std::auto_ptr<TextMessage> message;
        std::auto_ptr<Connection> connection(factory.createConnection(user, password));

        connection->start();

        std::auto_ptr<Session> session(connection->createSession());
        std::auto_ptr<Destination> dest(session->createTopic(destination));
        std::auto_ptr<MessageProducer> producer(session->createProducer(dest.get()));

        producer->setDeliveryMode(DeliveryMode::NON_PERSISTENT);

        for( int i=1; i <= messages; i ++) {
            message.reset(session->createTextMessage(body));
            producer->send(message.get());
            if( (i % 1000) == 0) {
                std::cout << "Sent " << i << " messages" << std::endl;;
            }
        }

        message.reset(session->createTextMessage("SHUTDOWN"));
        producer->send(message.get());

        connection->close();
    }

    std::cout << "-----------------------------------------------------\n";
    std::cout << "Finished with the example." << std::endl;
    std::cout << "=====================================================\n";

    activemq::library::ActiveMQCPP::shutdownLibrary();
}
Exemplo n.º 2
0
Arquivo: main.cpp Projeto: tacow/test
int main(int argc, char* argv[]) {
    if (argc < 4) {
        printf("%s [host:port] [queuename] [filename]\n", argv[0]);
        return 0;
    }
    const char* amqHost = argv[1];
    const char* queueName = argv[2];
    const char* filename = argv[3];

    try {
        ActiveMQCPP::initializeLibrary();
        char connStr[1024];
        snprintf(connStr, 1024, "tcp://%s", amqHost);
        ActiveMQConnectionFactory* connectionFactory = new ActiveMQConnectionFactory(connStr);
        Connection* connection = connectionFactory->createConnection();
        connection->start();
        Session* session = connection->createSession(Session::AUTO_ACKNOWLEDGE);
        Destination* destination = session->createQueue(queueName);
        MessageConsumer* consumer = session->createConsumer(destination);

        Message* message = consumer->receive();
        BytesMessage* bytesMessage = dynamic_cast<BytesMessage*>(message);
        if (bytesMessage) {
            FILE* f = fopen(filename, "w");
            if (f) {
                fwrite(bytesMessage->getBodyBytes(), 1, bytesMessage->getBodyLength(), f);
                fclose(f);
            } else {
                printf("Can't open file\n");
            }
        }
        delete message;

        delete destination;
        delete consumer;
        session->close();
        delete session;
        connection->close();
        delete connection;
        delete connectionFactory;
        ActiveMQCPP::shutdownLibrary();
    }
    catch (CMSException& e) {
        e.printStackTrace();
    }
    return 0;
}
Exemplo n.º 3
0
	void runConsumer() {

		try {

			// Create a ConnectionFactory
			ActiveMQConnectionFactory* connectionFactory =
				new ActiveMQConnectionFactory( brokerURI );

			// Create a Connection
			connection = connectionFactory->createConnection();
			delete connectionFactory;

			ActiveMQConnection* amqConnection = dynamic_cast<ActiveMQConnection*>( connection );
			if( amqConnection != NULL ) {
				amqConnection->addTransportListener( this );
			}

			connection->start();

			connection->setExceptionListener(this);

			// Create a Session
			if( clientAck ) {
				session = connection->createSession( Session::CLIENT_ACKNOWLEDGE );
			} else {
				session = connection->createSession( Session::AUTO_ACKNOWLEDGE );
			}

			// Create the destination (Topic or Queue)
			if( useTopic ) {
				destination = session->createTopic( destURI );
			} else {
				destination = session->createQueue( destURI );
			}

			// Create a MessageConsumer from the Session to the Topic or Queue
			consumer = session->createConsumer( destination );
			consumer->setMessageListener( this );

		} catch (CMSException& e) {
			e.printStackTrace();
		}
	}