Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final int SEND_NUM = 5;
- private static final String BROKER_URL = "tcp://localhost:61616";
- private static final String DESTINATION = "hoo.mq.queue";
- public static void sendMessage(QueueSession session, javax.jms.QueueSender sender) throws Exception {
- for (int i = 0; i < SEND_NUM; i++) {
- String message = "msg" + (i + 1) + "#";
- MapMessage map = session.createMapMessage();
- map.setString("text", message);
- map.setLong("time", System.currentTimeMillis());
- System.out.println(map);
- sender.send(map);
- //Thread.sleep(1000);
- }
- }
- public static void run() throws Exception {
- QueueConnection connection = null;
- QueueSession session = null;
- try {
- QueueConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, BROKER_URL);
- connection = factory.createQueueConnection();
- connection.start();
- session = connection.createQueueSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
- Queue queue = session.createQueue(DESTINATION);
- javax.jms.QueueSender sender = session.createSender(queue);
- sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- sendMessage(session, sender);
- session.commit();
- } catch (Exception e) {
- throw e;
- } finally {
- if (session != null) {
- session.close();
- }
- if (connection != null) {
- connection.close();
- }
- }
- }
- public static void main(String[] args) throws Exception {
- QueueSender.run();
- }
- private static final String BROKER_URL = "tcp://localhost:61616";
- private static final String TARGET = "hoo.mq.queue?customer.prefetchSize=400";
- public static void run() throws Exception {
- QueueConnection connection = null;
- QueueSession session = null;
- try {
- QueueConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,BROKER_URL);
- connection = factory.createQueueConnection();
- ((ActiveMQConnection)connection).setUseAsyncSend(true);
- connection.start();
- session = connection.createQueueSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
- Queue queue = session.createQueue(TARGET);
- javax.jms.QueueReceiver receiver = session.createReceiver(queue);
- receiver.setMessageListener(new MessageListener() {
- public void onMessage(Message msg) {
- if (msg != null) {
- MapMessage map = (MapMessage) msg;
- try {
- System.out.println(map.getLong("time") + "接收#" + map.getString("text"));
- } catch (JMSException e) {
- e.printStackTrace();
- }
- }
- }
- });
- Thread.sleep(1000*10);
- session.commit();
- } catch (Exception e) {
- throw e;
- } finally {
- if (session != null) {
- session.close();
- }
- if (connection != null) {
- connection.close();
- }
- }
- }
- public static void main(String[] args) throws Exception {
- QueueReceiver.run();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement