001: /*
002: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
003: * Government Rights - Commercial software. Government users are subject
004: * to the Sun Microsystems, Inc. standard license agreement and
005: * applicable provisions of the FAR and its supplements. Use is subject
006: * to license terms.
007: *
008: * This distribution may include materials developed by third parties.
009: * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
010: * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
011: * other countries.
012: *
013: * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
014: *
015: * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
016: * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
017: * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
018: * en vigueur de la FAR (Federal Acquisition Regulations) et des
019: * supplements a celles-ci. Distribue par des licences qui en
020: * restreignent l'utilisation.
021: *
022: * Cette distribution peut comprendre des composants developpes par des
023: * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
024: * sont des marques de fabrique ou des marques deposees de Sun
025: * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
026: */
027:
028: package simplemessageclient;
029:
030: import javax.jms.Connection;
031: import javax.jms.ConnectionFactory;
032: import javax.jms.Destination;
033: import javax.jms.JMSException;
034: import javax.jms.MessageProducer;
035: import javax.jms.Queue;
036: import javax.jms.Session;
037: import javax.jms.TextMessage;
038: import javax.naming.Context;
039: import javax.naming.InitialContext;
040: import javax.naming.NamingException;
041:
042: /**
043: *
044: * @author blaha
045: */
046: public class Main {
047:
048: /** Creates a new instance of Main */
049: public Main() {
050: }
051:
052: /**
053: * @param args the command line arguments
054: */
055: public static void main(String[] args) {
056: Context jndiContext = null;
057: ConnectionFactory connectionFactory = null;
058: Connection connection = null;
059: Session session = null;
060: Destination destination = null;
061: MessageProducer messageProducer = null;
062: TextMessage message = null;
063: final int NUM_MSGS = 3;
064:
065: try {
066: jndiContext = new InitialContext();
067: } catch (NamingException e) {
068: System.out.println("Could not create JNDI " + "context: "
069: + e.toString());
070: System.exit(1);
071: }
072:
073: try {
074: connectionFactory = (ConnectionFactory) jndiContext
075: .lookup("jms/SimpleMessageDestinationFactory");
076: destination = (Queue) jndiContext
077: .lookup("jms/SimpleMessageBean");
078: } catch (NamingException e) {
079: System.out.println("JNDI lookup failed: " + e.toString());
080: System.exit(1);
081: }
082:
083: try {
084: connection = connectionFactory.createConnection();
085: session = connection.createSession(false,
086: Session.AUTO_ACKNOWLEDGE);
087: messageProducer = session.createProducer(destination);
088: message = session.createTextMessage();
089:
090: for (int i = 0; i < NUM_MSGS; i++) {
091: message.setText("This is message " + (i + 1));
092: System.out.println("Sending message: "
093: + message.getText());
094: messageProducer.send(message);
095: }
096:
097: System.out
098: .println("To see if the bean received the messages,");
099: System.out
100: .println(" check <install_dir>/domains/domain1/logs/server.log.");
101: } catch (JMSException e) {
102: System.out.println("Exception occurred: " + e.toString());
103: } finally {
104: if (connection != null) {
105: try {
106: connection.close();
107: } catch (JMSException e) {
108: }
109: }
110: System.exit(0);
111: }
112: }
113:
114: }
|