最近整理的学习资料(Hadoop/MongoDB/ActiveMQ)汇总

来源:互联网 发布:dnf账号数据异常 编辑:程序博客网 时间:2024/05/17 00:54

1、Hadoop和MangoDB方面资料汇总(Download)


2、Mina Spring Hibernate Struts JDK JavaEE等chm格式文档(Download)


3、ActiveMQ学习资料(Download)推荐学习ActiveMQ博客:淮少吧

附入门示例(需要加入log4j-1.2.14.jar和activemq-all-5.6.0.jar):


package jms.start;/** * ActiveMQConfig * * @author Tom * @version ActiveMQConfig.java 2012-9-12 下午2:28:43 * */public final class ActiveMQConfig {    public final static String JMS_PROVIDER_ADDRESS = "tcp://192.168.6.45:61616";// 地址       public final static String DESTINATION_NAME = "demoQueue";}

package jms.start;import java.util.Date;import java.util.Random;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageProducer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory;public class Producer {    public static void main(String[] args) {        for (int i=0; i<500; i++) {            new Thread(new Runnable() {                @Override                public void run() {                    Producer.produce();                }            }).start();        }    }        private static void produce() {        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConfig.JMS_PROVIDER_ADDRESS);// 连接器           Connection connection = null;        Session session = null;        MessageProducer producer = null;        try {            connection = connectionFactory.createConnection();// 创建连接               session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);// 打开会话               Destination dest = session.createQueue(ActiveMQConfig.DESTINATION_NAME);// 消息目的地               producer = session.createProducer(dest);// 消息发送者               boolean flag = true;            int i = 0;            while (flag) {                if (i >= Integer.MAX_VALUE)                    flag = false;                try {                    Thread.sleep(new Random().nextInt(1000));                } catch (InterruptedException e) {                    e.printStackTrace();                }                Message message = session.createTextMessage(Thread.currentThread().getName() + i + " say hello at "+ new Date() + ".");// 消息                   producer.send(message);// 发送                   System.out.println(((TextMessage) message).getText());                i++;            }        } catch (JMSException e1) {            e1.printStackTrace();        } finally {            try {                producer.close();// 关闭                session.close();                connection.close();            } catch (JMSException e) {                e.printStackTrace();            }           }    }}

package jms.start;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageConsumer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory;public class Consumer {    public static void main(String[] args) {                for (int i=0; i<5; i++) {            new Thread(new Runnable() {                @Override                public void run() {                    Consumer.consume();                }            }).start();        }    }        private static void consume() {        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConfig.JMS_PROVIDER_ADDRESS);// 连接器           Connection connection = null;        Session session = null;        MessageConsumer consumer = null;        try {            connection = connectionFactory.createConnection();// 创建连接               session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);// 打开会话               Destination dest = session.createQueue(ActiveMQConfig.DESTINATION_NAME);// 消息目的地               consumer = session.createConsumer(dest);            connection.start();            boolean flag = true;            while (flag) {                Message message = consumer.receive();                TextMessage textMessage = (TextMessage) message;                String text = textMessage.getText();                System.out.println("从ActiveMQ取回一条消息: " + text);            }        } catch (JMSException e) {            e.printStackTrace();        } finally {            try {                consumer.close();                session.close();                connection.close();            } catch (JMSException e) {                e.printStackTrace();            }        }    }}





原创粉丝点击