1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright (c) 2000-2017 Oracle and/or its affiliates. All rights reserved.
5  *
6  * The contents of this file are subject to the terms of either the GNU
7  * General Public License Version 2 only ("GPL") or the Common Development
8  * and Distribution License("CDDL") (collectively, the "License").  You
9  * may not use this file except in compliance with the License.  You can
10  * obtain a copy of the License at
11  * https://oss.oracle.com/licenses/CDDL+GPL-1.1
12  * or LICENSE.txt.  See the License for the specific
13  * language governing permissions and limitations under the License.
14  *
15  * When distributing the software, include this License Header Notice in each
16  * file and include the License file at LICENSE.txt.
17  *
18  * GPL Classpath Exception:
19  * Oracle designates this particular file as subject to the "Classpath"
20  * exception as provided by Oracle in the GPL Version 2 section of the License
21  * file that accompanied this code.
22  *
23  * Modifications:
24  * If applicable, add the following below the License Header, with the fields
25  * enclosed by brackets [] replaced by your own identifying information:
26  * "Portions Copyright [year] [name of copyright owner]"
27  *
28  * Contributor(s):
29  * If you wish your version of this file to be governed by only the CDDL or
30  * only the GPL Version 2, indicate your decision by adding "[Contributor]
31  * elects to include this software in this distribution under the [CDDL or GPL
32  * Version 2] license."  If you don't indicate a single choice of license, a
33  * recipient has the option to distribute your version of this file under
34  * either the CDDL, the GPL Version 2 or to extend the choice of license to
35  * its licensees as provided above.  However, if you add GPL Version 2 code
36  * and therefore, elected the GPL Version 2 license, then the option applies
37  * only if the new code is made subject to such option by the copyright
38  * holder.
39  */
40 
41 import javax.jms.*;
42 
43 /**
44  * The MessagePropertiesProducer.class sends messages to a queue with
45  * various types of message properties set on producer
46  * <p>
47  * Run this program in conjunction with MessagePropertiesConsumer.
48  * Specify a queue name on the command line when you run
49  * the program.
50  */
51 
52 public class MessagePropertiesProducer {
53 
54 	private String destName          = null;
55 	static int exitcode = 0;
56 
57 	/**
58         * Main method.
59         *
60         * @param args      the queue used by the example
61         */
main(String args[])62         public static void main(String args[]) {
63 
64 		if ( args.length < 1 ) {
65                   System.out.println("Usage: java MessagePropertiesProducer <queue_name> ");
66                   System.exit(1);
67                 }
68 
69 		// Send messages to queue with message properties set.
70                 MessagePropertiesProducer msgPropertiesProducer = new MessagePropertiesProducer();
71 		msgPropertiesProducer.parseArgs(args);
72 
73                 try {
74                         msgPropertiesProducer.runTest();
75                 }catch(JMSException ex) {
76                         ex.printStackTrace();
77                         exitcode = 1;
78                 }
79                 System.exit(exitcode);
80         }
81 
82 	/**
83         * parseArgs method.
84         *
85         * @param args  the arguments that are passed through main method
86         */
parseArgs(String[] args)87 	public void parseArgs(String[] args){
88 
89                 destName = new String(args[0]);
90                 System.out.println("Queue name is " + destName);
91         }
92 
93 
94 	/**
95 	 * JMSProducer method send(Destination destination, Message message),
96 	 * with a TextMessage ensuring that you can set message properties
97 	 *
98 	 * @param  none
99 	 * @throws JMSException
100 	 */
runTest()101 	private void runTest() throws JMSException {
102 
103 		String uniqueID = Long.toString(System.currentTimeMillis());
104 
105 		boolean booleanVal = true;
106 		byte byteVal = 7;
107 		short shortVal = 123;
108 		int intVal = 1357924680;
109 		long longVal = 84838481357924680L;
110 		float floatVal = 3.1415926535f;
111 		double doubleVal = 2.71828182846d;
112 		String stringVal = "Hello";
113 
114 		// send a message
115 		{
116 			ConnectionFactory connectionFactory = new com.sun.messaging.ConnectionFactory();
117 			JMSContext context = connectionFactory.createContext();
118 			JMSProducer producer = context.createProducer();
119 			System.out.println("Set properties on producer");
120 			// set properties
121 
122 			// boolean
123 			System.out.println("Set boolean property on producer");
124 			producer.setProperty("booleanProp", booleanVal);
125 			System.out.println( "booleanProp on producer through getObjectProperty :" + producer.getObjectProperty("booleanProp"));
126 			System.out.println( "booleanProp on producer through getBooleanProperty :" + producer.getBooleanProperty("booleanProp"));
127 
128 			// byte
129 			System.out.println("Set byte property on producer");
130 			producer.setProperty("byteProp", byteVal);
131 			System.out.println( "byteProp on producer through getObjectProperty :" +producer.getObjectProperty("byteProp"));
132 			System.out.println( "byteProp on producer through getBytesProperty :" +producer.getByteProperty("byteProp"));
133 
134 			// short
135 			System.out.println("Set short property on producer");
136 			producer.setProperty("shortProp", shortVal);
137 			System.out.println( "shortProp on producer through getObjectProperty :" +producer.getObjectProperty("shortProp"));
138                         System.out.println( "shortProp on producer through getShortProperty :" +producer.getShortProperty("shortProp"));
139 
140 			// int
141 			System.out.println("Set int property on producer");
142 			producer.setProperty("intProp", intVal);
143                         System.out.println( "intProp on producer through getObjectProperty :" +producer.getObjectProperty("intProp"));
144                         System.out.println( "intProp on producer through getIntProperty :" +producer.getIntProperty("intProp"));
145 
146 			// long
147 			System.out.println("Set long property on producer");
148                         producer.setProperty("longProp", longVal);
149                         System.out.println( "longProp on producer through getObjectProperty :" +producer.getObjectProperty("longProp"));
150                         System.out.println( "longProp on producer through getLongProperty :" +producer.getLongProperty("longProp"));
151 
152 			// float
153 			System.out.println("Set long property on producer");
154 			producer.setProperty("floatProp", floatVal);
155                         System.out.println( "floatProp on producer through getObjectProperty :" +producer.getObjectProperty("floatProp"));
156                         System.out.println( "floatProp on producer through getFloatProperty :" +producer.getFloatProperty("floatProp"));
157 
158 			// double
159 			System.out.println("Set double property on producer");
160 			producer.setProperty("doubleProp", doubleVal);
161 			System.out.println( "doubleProp on producer through getObjectProperty :" +producer.getObjectProperty("doubleProp"));
162                         System.out.println( "doubleProp on producer through getDoubleProperty :" +producer.getDoubleProperty("doubleProp"));
163 
164 			// String
165 			System.out.println("Set String property on producer");
166 			producer.setProperty("stringProp", stringVal);
167 			System.out.println( "stringProp on producer through getObjectProperty :" +producer.getObjectProperty("stringProp"));
168                         System.out.println( "stringProp on producer through getStringProperty :" +producer.getStringProperty("stringProp"));
169 
170 			// now send message
171 			producer.send(context.createQueue(destName),context.createTextMessage(uniqueID));
172 			System.out.println("Message "+uniqueID+" sent successfully");
173 			context.close();
174 		}
175 
176 
177 	}
178 
179 
180 }
181