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), JMSConstants.DESTINATION_TYPE_QUEUE,
088: bm);
089: connection.close();
090: }
091:
092: private void sendTextMessage(String destName, String payload)
093: throws Exception {
094: InitialContext ic = getInitialContext();
095: ConnectionFactory confac = (ConnectionFactory) ic
096: .lookup("ConnectionFactory");
097: Connection connection = JMSUtils.createConnection(confac, null,
098: null, JMSConstants.DESTINATION_TYPE_QUEUE);
099: Session session = JMSUtils.createSession(connection, false,
100: Session.AUTO_ACKNOWLEDGE,
101: JMSConstants.DESTINATION_TYPE_QUEUE);
102:
103: TextMessage tm = session.createTextMessage(payload);
104: JMSUtils.sendMessageToJMSDestination(session, (Destination) ic
105: .lookup(destName), JMSConstants.DESTINATION_TYPE_QUEUE,
106: tm);
107: connection.close();
108: }
109:
110: private InitialContext getInitialContext() throws NamingException {
111: Properties env = new Properties();
112: if (System.getProperty("java.naming.provider.url") == null) {
113: env
114: .put("java.naming.provider.url",
115: "tcp://localhost:61616");
116: }
117: if (System.getProperty("java.naming.factory.initial") == null) {
118: env
119: .put("java.naming.factory.initial",
120: "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
121: }
122: return new InitialContext(env);
123: }
124:
125: public static byte[] getBytesFromFile(String fileName)
126: throws IOException {
127:
128: File file = new File(fileName);
129: InputStream is = new FileInputStream(file);
130: long length = file.length();
131:
132: byte[] bytes = new byte[(int) length];
133:
134: int offset = 0;
135: int numRead = 0;
136: while (offset < bytes.length
137: && (numRead = is.read(bytes, offset, bytes.length
138: - offset)) >= 0) {
139: offset += numRead;
140: }
141:
142: // Ensure all the bytes have been read in
143: if (offset < bytes.length) {
144: throw new IOException("Could not completely read file "
145: + file.getName());
146: }
147:
148: is.close();
149: return bytes;
150: }
151:
152: private static double getRandom(double base, double varience,
153: boolean onlypositive) {
154: double rand = Math.random();
155: return (base + ((rand > 0.5 ? 1 : -1) * varience * base * rand))
156: * (onlypositive ? 1 : (rand > 0.5 ? 1 : -1));
157: }
158:
159: }
|