2006-03-01 09:59:03 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2006 The Apache Software Foundation or its licensors, as
|
|
|
|
* applicable.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2006-03-01 09:27:46 -05:00
|
|
|
#include <activemq/ActiveMQTextMessage.h>
|
2006-04-23 18:09:13 -04:00
|
|
|
#include <activemq/ActiveMQBytesMessage.h>
|
2006-03-01 09:27:46 -05:00
|
|
|
#include <cms/TopicConnectionFactory.h>
|
|
|
|
#include <cms/TopicConnection.h>
|
|
|
|
#include <cms/TopicSession.h>
|
|
|
|
#include <cms/TopicSubscriber.h>
|
|
|
|
#include <cms/Topic.h>
|
|
|
|
#include <activemq/ActiveMQConnectionFactory.h>
|
|
|
|
#include <cms/MessageListener.h>
|
|
|
|
#include <cms/ExceptionListener.h>
|
|
|
|
#include <cms/TopicPublisher.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
class Tester
|
|
|
|
:
|
|
|
|
public cms::ExceptionListener,
|
|
|
|
public cms::MessageListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
Tester(){numReceived = 0;}
|
|
|
|
virtual ~Tester(){}
|
|
|
|
|
|
|
|
void test(){
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
2006-04-23 18:09:13 -04:00
|
|
|
int messagesPerType = 3000;
|
|
|
|
int sleepTime = 2;
|
2006-03-01 09:27:46 -05:00
|
|
|
|
2006-04-23 18:09:13 -04:00
|
|
|
printf("Starting activemqcms test (sending %d messages per type and sleeping %d seconds) ...\n", messagesPerType, sleepTime );
|
2006-03-01 09:27:46 -05:00
|
|
|
|
2006-03-01 09:59:03 -05:00
|
|
|
// START SNIPPET: demo
|
2006-04-23 18:09:13 -04:00
|
|
|
cms::TopicConnectionFactory* connectionFactory = new activemq::ActiveMQConnectionFactory( "127.0.0.1:61613" );
|
2006-03-01 09:27:46 -05:00
|
|
|
cms::TopicConnection* connection = connectionFactory->createTopicConnection();
|
|
|
|
connection->setExceptionListener( this );
|
|
|
|
connection->start();
|
|
|
|
cms::TopicSession* session = connection->createTopicSession( false );
|
|
|
|
cms::Topic* topic = session->createTopic("mytopic");
|
|
|
|
cms::TopicSubscriber* subscriber = session->createSubscriber( topic );
|
|
|
|
subscriber->setMessageListener( this );
|
|
|
|
cms::TopicPublisher* publisher = session->createPublisher( topic );
|
|
|
|
|
2006-04-23 18:09:13 -04:00
|
|
|
// Send some text messages.
|
2006-03-01 09:27:46 -05:00
|
|
|
const char* text = "this is a test!";
|
2006-04-23 18:09:13 -04:00
|
|
|
cms::TextMessage* textMsg = session->createTextMessage( text );
|
|
|
|
for( int ix=0; ix<messagesPerType; ++ix ){
|
|
|
|
publisher->publish( textMsg );
|
|
|
|
doSleep();
|
2006-03-01 09:27:46 -05:00
|
|
|
}
|
2006-04-23 18:09:13 -04:00
|
|
|
|
|
|
|
// Send some bytes messages.
|
|
|
|
char buf[10];
|
|
|
|
memset( buf, 0, 10 );
|
|
|
|
buf[0] = 0;
|
|
|
|
buf[1] = 1;
|
|
|
|
buf[2] = 2;
|
|
|
|
buf[3] = 3;
|
|
|
|
buf[4] = 0;
|
|
|
|
buf[5] = 4;
|
|
|
|
buf[6] = 5;
|
|
|
|
buf[7] = 6;
|
|
|
|
cms::BytesMessage* bytesMsg = session->createBytesMessage();
|
|
|
|
bytesMsg->setData( buf, 10 );
|
|
|
|
for( int ix=0; ix<messagesPerType; ++ix ){
|
|
|
|
publisher->publish( bytesMsg );
|
|
|
|
doSleep();
|
|
|
|
}
|
2006-03-01 09:59:03 -05:00
|
|
|
// END SNIPPET: demo
|
2006-03-01 09:27:46 -05:00
|
|
|
|
|
|
|
sleep( sleepTime );
|
|
|
|
|
|
|
|
printf("received: %d\n", numReceived );
|
|
|
|
|
2006-04-23 18:09:13 -04:00
|
|
|
sleep( 5 );
|
|
|
|
|
|
|
|
printf("unsubscribing\n" );
|
|
|
|
delete publisher;
|
2006-03-01 09:27:46 -05:00
|
|
|
subscriber->close();
|
|
|
|
delete subscriber;
|
2006-04-23 18:09:13 -04:00
|
|
|
|
|
|
|
sleep( 5 );
|
|
|
|
|
2006-03-01 09:27:46 -05:00
|
|
|
session->close();
|
|
|
|
delete session;
|
|
|
|
connection->close();
|
|
|
|
delete connection;
|
|
|
|
delete connectionFactory;
|
|
|
|
|
|
|
|
}catch( cms::CMSException& ex ){
|
|
|
|
printf("StompTester::test() - %s\n", ex.getMessage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onMessage( const cms::Message* message ){
|
2006-04-23 18:09:13 -04:00
|
|
|
|
|
|
|
// Got a text message.
|
2006-03-01 09:27:46 -05:00
|
|
|
const cms::TextMessage* txtMsg = dynamic_cast<const cms::TextMessage*>(message);
|
2006-04-23 18:09:13 -04:00
|
|
|
if( txtMsg != NULL ){
|
|
|
|
//printf("received text msg: %s\n", txtMsg->getText() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Got a bytes msg.
|
|
|
|
const cms::BytesMessage* bytesMsg = dynamic_cast<const cms::BytesMessage*>(message);
|
|
|
|
if( bytesMsg != NULL ){
|
|
|
|
/*printf("received bytes msg: " );
|
|
|
|
const char* bytes = bytesMsg->getData();
|
|
|
|
int numBytes = bytesMsg->getNumBytes();
|
|
|
|
for( int ix=0; ix<numBytes; ++ix ){
|
|
|
|
printf("[%d]", bytes[ix] );
|
|
|
|
}
|
|
|
|
printf("\n");*/
|
2006-03-01 09:27:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
numReceived++;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onException( const cms::CMSException* error ){
|
|
|
|
printf( "StompTester::onException() - %s\n", error->getMessage() );
|
|
|
|
}
|
|
|
|
|
2006-03-01 09:59:03 -05:00
|
|
|
virtual void doSleep() {
|
|
|
|
timespec sleepTime;
|
|
|
|
sleepTime.tv_sec = 0;
|
|
|
|
sleepTime.tv_nsec = 1000;
|
|
|
|
nanosleep( &sleepTime, &sleepTime );
|
|
|
|
}
|
|
|
|
|
2006-03-01 09:27:46 -05:00
|
|
|
private:
|
|
|
|
|
|
|
|
int numReceived;
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]){
|
|
|
|
try{
|
|
|
|
|
|
|
|
Tester tester;
|
|
|
|
tester.test();
|
|
|
|
|
|
|
|
}catch( ... ){
|
|
|
|
printf("main - caught unknown exception\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("done");
|
|
|
|
|
|
|
|
cout.flush();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|