Thursday, October 12, 2006

Re: [EZLINK] need jms help

Hi Swamy,

This sample is demonstrated using TIBCO EMS Server

Publisher

import javax.jms.*;
import javax.naming.*;
import java.util.Vector;
public class tibjmsTopicPublisher
{
String serverUrl = null;
String userName = null;
String password = null;
String topicName = "topic.sample";
Vector data = new Vector();

public tibjmsTopicPublisher(String[] args) {
parseArgs(args);
/* print parameters */
System.out.println("\n------------------------------------------------------------------------");
System.out.println("tibjmsTopicPublisher SAMPLE");
System.out.println("------------------------------------------------------------------------");
System.out.println("Server....................... "+(serverUrl!=null?serverUrl:"localhost"));
System.out.println("User......................... "+(userName!=null?userName:"(null)"));
System.out.println("Topic........................ "+topicName);
System.out.println("Message Text................. ");
for(int i=0;i<data.size();i++)
{
System.out.println("\t"+data.elementAt(i));
}
System.out.println("------------------------------------------------------------------------\n");
try
{
tibjmsUtilities.initSSLParams(serverUrl,args);
}
catch (JMSSecurityException e)
{
System.err.println("JMSSecurityException: "+e.getMessage()+", provider="+e.getErrorCode());
e.printStackTrace();
System.exit(0);
}
if (topicName == null)
{
System.err.println("Error: must specify topic name");
usage();
}
if (data.size() == 0)
{
System.err.println("Error: must specify at least one message text");
usage();
}
System.err.println("Publishing on topic '"+topicName+"'\n");
try
{
TopicConnectionFactory factory = new com.tibco.tibjms.TibjmsTopicConnectionFactory(serverUrl);
TopicConnection connection = factory.createTopicConnection(userName,password);
TopicSession session = connection.createTopicSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
/*
* Use createTopic() to enable publishing into dynamic topics.
*/
javax.jms.Topic topic = session.createTopic(topicName);
TopicPublisher publisher = session.createPublisher(topic);
/* publish messages */
for (int i=0; i<data.size(); i++)
{
javax.jms.TextMessage message = session.createTextMessage();
String text = (String)data.elementAt(i);
message.setText(text);
publisher.publish(message);
System.err.println("Published message: "+text);
}
connection.close();
}
catch(JMSException e)
{
e.printStackTrace();
System.exit(0);
}
}
public static void main(String args[])
{
tibjmsTopicPublisher t = new tibjmsTopicPublisher(args);
}
void usage()
{
System.err.println("\nUsage: java tibjmsTopicPublisher [options]");
System.err.println(" <message-text-1>");
System.err.println(" [<message-text-2>] ...");
System.err.println("");
System.err.println(" where options are:");
System.err.println("");
System.err.println(" -server <server URL> - EMS server URL, default is local server");
System.err.println(" -user <user name> - user name, default is null");
System.err.println(" -password <password> - password, default is null");
System.err.println(" -topic <topic-name> - topic name, default is \"topic.sample\"");
System.err.println(" -help-ssl - help on ssl parameters\n");
System.exit(0);
}
void parseArgs(String[] args)
{
int i=0;
while(i < args.length)
{
if (args[i].compareTo("-server")==0)
{
if ((i+1) >= args.length) usage();
serverUrl = args[i+1];
i += 2;
}
else
if (args[i].compareTo("-topic")==0)
{
if ((i+1) >= args.length) usage();
topicName = args[i+1];
i += 2;
}
else
if (args[i].compareTo("-user")==0)
{
if ((i+1) >= args.length) usage();
userName = args[i+1];
i += 2;
}
else
if (args[i].compareTo("-password")==0)
{
if ((i+1) >= args.length) usage();
password = args[i+1];
i += 2;
}
else
if (args[i].compareTo("-help")==0)
{
usage();
}
else
if (args[i].compareTo("-help-ssl")==0)
{
tibjmsUtilities.sslUsage();
}
else
if(args[i].startsWith("-ssl"))
{
i += 2;
}
else
{
data.addElement(args[i]);
i++;
}
}
}
}


Subscriber

import javax.jms.*;
import javax.naming.*;
public class tibjmsTopicSubscriber
{
String serverUrl = null;
String userName = null;
String password = null;
String topicName = "topic.sample";
public tibjmsTopicSubscriber(String[] args) {
parseArgs(args);
/* print parameters */
System.err.println("\n------------------------------------------------------------------------");
System.err.println("tibjmsTopicSubscriber SAMPLE");
System.err.println("------------------------------------------------------------------------");
System.err.println("Server....................... "+((serverUrl != null)?serverUrl:"localhost"));
System.err.println("User......................... "+((userName != null)?userName:"(null)"));
System.err.println("Topic........................ "+topicName);
System.err.println("------------------------------------------------------------------------\n");
try
{
tibjmsUtilities.initSSLParams(serverUrl,args);
}
catch (JMSSecurityException e)
{
System.err.println("JMSSecurityException: "+e.getMessage()+", provider="+e.getErrorCode());
e.printStackTrace();
System.exit(0);
}

if (topicName == null) {
System.err.println("Error: must specify topic name");
usage();
}
System.err.println("Subscribing to topic: "+topicName);
try
{
TopicConnectionFactory factory = new com.tibco.tibjms.TibjmsTopicConnectionFactory(serverUrl);
TopicConnection connection = factory.createTopicConnection(userName,password);
TopicSession session = connection.createTopicSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);
/*
* Use createTopic() to enable subscriptions to dynamic topics.
*/
javax.jms.Topic topic = session.createTopic(topicName);
TopicSubscriber subscriber = session.createSubscriber(topic);
connection.start();
/* read topic messages */
while(true)
{
javax.jms.Message message = subscriber.receive();
if (message == null)
break;
System.err.println("Received message: "+ message);

}

connection.close();
}
catch(JMSException e)
{
System.err.println("JMSException: "+e.getMessage()+", provider="+e.getErrorCode());
e.printStackTrace();
System.exit(0);
}
}
public static void main(String args[])
{
tibjmsTopicSubscriber t = new tibjmsTopicSubscriber(args);
}
void usage()
{
System.err.println("\nUsage: java tibjmsTopicSubscriber [options]");
System.err.println("");
System.err.println(" where options are:");
System.err.println("");
System.err.println(" -server <server URL> - EMS server URL, default is local server");
System.err.println(" -user <user name> - user name, default is null");
System.err.println(" -password <password> - password, default is null");
System.err.println(" -topic <topic-name> - topic name, default is \"topic.sample\"");
System.err.println(" -help-ssl - help on ssl parameters\n");
System.exit(0);
}
void parseArgs(String[] args)
{
int i=0;
while(i < args.length)
{
if (args[i].compareTo("-server")==0)
{
if ((i+1) >= args.length) usage();
serverUrl = args[i+1];
i += 2;
}
else
if (args[i].compareTo("-topic")==0)
{
if ((i+1) >= args.length) usage();
topicName = args[i+1];
i += 2;
}
else
if (args[i].compareTo("-user")==0)
{
if ((i+1) >= args.length) usage();
userName = args[i+1];
i += 2;
}
else
if (args[i].compareTo("-password")==0)
{
if ((i+1) >= args.length) usage();
password = args[i+1];
i += 2;
}
else
if (args[i].compareTo("-help")==0)
{
usage();
}
else
if (args[i].compareTo("-help-ssl")==0)
{
tibjmsUtilities.sslUsage();
}
else
if(args[i].startsWith("-ssl"))
{
i += 2;
}
else
{
System.err.println("Unrecognized parameter: "+args[i]);
usage();
}
}
}
}


brahmam nannapaneni <swamy_n99@yahoo.com.sg> wrote:
Hi All,

If any body knows JMS publish/subscribe domain model with multiple subscribers, will u please send me sample code or simple example. Please this is little bit urgent for me. please help me.


Thanks & Regards,
Swamy


---------------------------------

Real people. Real questions. Real answers. Share what you know.

[Non-text portions of this message have been removed]


---------------------------------
How low will we go? Check out Yahoo! Messenger's low PC-to-Phone call rates.

[Non-text portions of this message have been removed]



Yahoo! Groups Links

<*> To visit your group on the web, go to:

http://groups.yahoo.com/group/ezlink/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:

http://groups.yahoo.com/group/ezlink/join

(Yahoo! ID required)

<*> To change settings via email:
mailto:ezlink-digest@yahoogroups.com
mailto:ezlink-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
ezlink-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:

http://docs.yahoo.com/info/terms/