ActiveMQ---遇到的args输入问题
来源:互联网 发布:希区柯克剧场 知乎 编辑:程序博客网 时间:2024/04/30 19:28
最近在学习ActiveMQ,书上一个例子是演示pub/sub原理的stock portfolio.奇怪的是,运行Publisher后控制台只显示连接成功的信息,并没有哗哗哗地刷股票信息。
仔细看了主函数后才发现它是要有输入的,但不是运行后从控制台输入。而是通过args输入。
Publisher主函数代码如下
import java.util.Hashtable;import java.util.Map;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MapMessage;import javax.jms.Message;import javax.jms.MessageProducer;import javax.jms.Session;import org.apache.activemq.ActiveMQConnectionFactory;import org.apache.activemq.command.ActiveMQMapMessage;public class Publisher { protected int MAX_DELTA_PERCENT = 1; protected Map<String, Double> LAST_PRICES = new Hashtable<String, Double>();//前一次的股票价格 protected static int count = 10; protected static int total; protected static String brokerURL = "tcp://localhost:61616"; protected static transient ConnectionFactory factory; protected transient Connection connection; protected transient Session session; protected transient MessageProducer producer; public Publisher() throws JMSException { factory = new ActiveMQConnectionFactory(brokerURL); connection = factory.createConnection(); try { connection.start(); } catch (JMSException jmse) { connection.close(); throw jmse; } session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); producer = session.createProducer(null); } public void close() throws JMSException { if (connection != null) { connection.close(); } } public static void main(String[] args) throws JMSException { Publisher publisher = new Publisher(); while (total < 1000) { for (int i = 0; i < count; i++) { publisher.sendMessage(args); } total += count; System.out.println("Published '" + count + "' of '" + total + "' price messages"); try { Thread.sleep(1000); } catch (InterruptedException x) { } } publisher.close(); } protected void sendMessage(String[] stocks) throws JMSException { int idx = 0; while (true) { idx = (int)Math.round(stocks.length * Math.random()); if (idx < stocks.length) { break; } } String stock = stocks[idx]; Destination destination = session.createTopic("STOCKS." + stock); Message message = createStockMessage(stock, session); System.out.println("Sending: " + ((ActiveMQMapMessage)message).getContentMap() + " on destination: " + destination); producer.send(destination, message); } protected Message createStockMessage(String stock, Session session) throws JMSException { Double value = LAST_PRICES.get(stock); if (value == null) { value = new Double(Math.random() * 100); } // lets mutate the value by some percentage double oldPrice = value.doubleValue(); value = new Double(mutatePrice(oldPrice)); LAST_PRICES.put(stock, value); double price = value.doubleValue(); double offer = price * 1.001; boolean up = (price > oldPrice); MapMessage message = session.createMapMessage(); message.setString("stock", stock); message.setDouble("price", price); message.setDouble("offer", offer); message.setBoolean("up", up); return message; } protected double mutatePrice(double price) { double percentChange = (2 * Math.random() * MAX_DELTA_PERCENT) - MAX_DELTA_PERCENT; return price * (100 + percentChange) / 100; }}
eclipse添加args参数的方法
1.右击执行文件,如本例中的Publisher.java
run as 选中 Run Configuration…
2.选中第二个标签Arguments,在Program argument 内输入参数
参数用空格隔开
简单的例子
public class test { public static void main(String[] args){ for(String str:args){ System.out.println(str); } }}
0 0
- ActiveMQ---遇到的args输入问题
- C#的MainString args参数输入问题
- activemq时遇到的问题
- activeMq遇到的一些问题
- ActiveMQ中遇到的问题
- ActiveMQ 安装使用时遇到的问题
- ActiveMQ安装及启动遇到的问题
- netbeans 实现args的输入
- java中用到args[0]和args[1]做参数时遇到的小问题,和大家分享
- activemq 遇到的不能消息确认的问题。
- args.get_error的详细解释和在asp.net ajax中调用webservice遇到的问题
- python中的含有 *args 和**kwargs的用法 作为参数的函数,参数输入问题
- JAVA输入流 遇到的问题.
- args问题
- eclipse 中如何输入main(String[] args)的参数
- Activemq死锁的问题
- 对int char 输入遇到的问题 的探讨
- 使用scanf字符输入时遇到的问题
- 再论WGS84、ITRF2008及NavCom StarFire
- 共享单车非共享经济!!!
- 开始学习java和oracle
- [故障公告]博客站点遭遇超过20G的流量攻击被阿里云屏蔽
- 设计模式之单例模式
- ActiveMQ---遇到的args输入问题
- 《剑指offer》—11、二进制中1的个数
- Struts常量 Struts2 action 定义 配置 操作内置对象
- Python实现通讯录(1)
- qmake生成vs的vcproj/sln工程
- linux Nginx 安装
- Git常用命令快速一览
- 公共网络开放的WebService接口
- JS动态添加option和删除option