001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package samples.userguide;
021:
022: import org.apache.synapse.transport.jms.JMSConstants;
023: import org.apache.synapse.transport.jms.JMSUtils;
024:
025: import javax.jms.*;
026: import javax.naming.InitialContext;
027: import javax.naming.NamingException;
028: import java.util.Properties;
029: import java.io.File;
030: import java.io.FileInputStream;
031: import java.io.InputStream;
032: import java.io.IOException;
033:
034: public class GenericJMSClient {
035:
036: private static String getProperty(String name, String def) {
037: String result = System.getProperty(name);
038: if (result == null || result.length() == 0) {
039: result = def;
040: }
041: return result;
042: }
043:
044: public static void main(String[] args) throws Exception {
045:
046: String dest = getProperty("jms_dest",
047: "dynamicQueues/JMSTextProxy");
048: String type = getProperty("jms_type", "text");
049: String param = getProperty("jms_payload", getRandom(100, 0.9,
050: true)
051: + " " + (int) getRandom(10000, 1.0, true) + " IBM");
052:
053: GenericJMSClient app = new GenericJMSClient();
054: if ("text".equalsIgnoreCase(type)) {
055: app.sendTextMessage(dest, param);
056: } else if ("binary".equalsIgnoreCase(type)) {
057: app.sendBytesMessage(dest, getBytesFromFile(param));
058: } else if ("pox".equalsIgnoreCase(type)) {
059: app.sendTextMessage(dest,
060: "<m:placeOrder xmlns:m=\"http://services.samples/xsd\">\n"
061: + " <m:order>\n" + " <m:price>"
062: + getRandom(100, 0.9, true)
063: + "</m:price>\n" + " <m:quantity>"
064: + (int) getRandom(10000, 1.0, true)
065: + "</m:quantity>\n" + " <m:symbol>"
066: + param + "</m:symbol>\n"
067: + " </m:order>\n" + "</m:placeOrder>");
068: } else {
069: System.out.println("Unknown JMS message type");
070: }
071: }
072:
073: private void sendBytesMessage(String destName, byte[] payload)
074: throws Exception {
075: InitialContext ic = getInitialContext();
076: ConnectionFactory confac = (ConnectionFactory) ic
077: .lookup("ConnectionFactory");
078: Connection connection = JMSUtils.createConnection(confac, null,
079: null, JMSConstants.DESTINATION_TYPE_QUEUE);
080: Session session = JMSUtils.createSession(connection, false,
081: Session.AUTO_ACKNOWLEDGE,
082: JMSConstants.DESTINATION_TYPE_QUEUE);
083:
084: BytesMessage bm = session.createBytesMessage();
085: bm.writeBytes(payload);
086: JMSUtils.sendMessageToJMSDestination(session, (Destination) ic
087: .lookup(destName), bm);
088: connection.close();
089: }
090:
091: private void sendTextMessage(String destName, String payload)
092: throws Exception {
093: InitialContext ic = getInitialContext();
094: ConnectionFactory confac = (ConnectionFactory) ic
095: .lookup("ConnectionFactory");
096: Connection connection = JMSUtils.createConnection(confac, null,
097: null, JMSConstants.DESTINATION_TYPE_QUEUE);
098: Session session = JMSUtils.createSession(connection, false,
099: Session.AUTO_ACKNOWLEDGE,
100: JMSConstants.DESTINATION_TYPE_QUEUE);
101:
102: TextMessage tm = session.createTextMessage(payload);
103: JMSUtils.sendMessageToJMSDestination(session, (Destination) ic
104: .lookup(destName), tm);
105: connection.close();
106: }
107:
108: private InitialContext getInitialContext() throws NamingException {
109: Properties env = new Properties();
110: if (System.getProperty("java.naming.provider.url") == null) {
111: env
112: .put("java.naming.provider.url",
113: "tcp://localhost:61616");
114: }
115: if (System.getProperty("java.naming.factory.initial") == null) {
116: env
117: .put("java.naming.factory.initial",
118: "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
119: }
120: return new InitialContext(env);
121: }
122:
123: public static byte[] getBytesFromFile(String fileName)
124: throws IOException {
125:
126: File file = new File(fileName);
127: InputStream is = new FileInputStream(file);
128: long length = file.length();
129:
130: byte[] bytes = new byte[(int) length];
131:
132: int offset = 0;
133: int numRead = 0;
134: while (offset < bytes.length
135: && (numRead = is.read(bytes, offset, bytes.length
136: - offset)) >= 0) {
137: offset += numRead;
138: }
139:
140: // Ensure all the bytes have been read in
141: if (offset < bytes.length) {
142: throw new IOException("Could not completely read file "
143: + file.getName());
144: }
145:
146: is.close();
147: return bytes;
148: }
149:
150: private static double getRandom(double base, double varience,
151: boolean onlypositive) {
152: double rand = Math.random();
153: return (base + ((rand > 0.5 ? 1 : -1) * varience * base * rand))
154: * (onlypositive ? 1 : (rand > 0.5 ? 1 : -1));
155: }
156:
157: }
|