`

(转)JBOSS7.1下开发JMS(HoernetQ)的示例DEMO

阅读更多

1.首先是要add-user.bat,添加用户,在jboss的bin目录下运行add-user.bat

 

角色是guest不是gutst。照样添加就可以了.之后就是一路yes敲下去.

2.替换XML文件,CMD命令我用的不熟,折腾半天也没运行起JBOSS,伤心之余就跑去更换JBOSS的启动使用的XML文件了.D:\software\jboss-as-7.1.3.Final\standalone\configuration文件夹下,默认使用standalone.xml,你把他备份好,然后把standalone-full.xml的名字改为standalone.xml.因为那个啥,JBOSS7默认是没带JMS的.

3.修改Jboss配置文件standalone-full.xml(也就是现在的standalone.xml),找到hornetq-server节点,在该节点下的jms-destinations确定含有以下配置.

 

1
2
3
4
5
6
7
8
9
10
<jms-destinations>
                    <jms-queue name="testQueue">
                        <entry name="queue/test"/>
                        <entry name="java:jboss/exported/jms/queue/test"/>
                    </jms-queue>
                    <jms-topic name="ServerNotificationTopic">
                        <entry name="topic/ServerNotification"/>
                        <entry name="java:jboss/exported/jms/topic/ServerNotification"/>
                    </jms-topic>
                </jms-destinations>

没就加上.

4.进入JBOSS控制台,看到JMS就是正确的了.擦,我的standalone.bat无法运行..才发现.不过Eclipse运行貌似一点问题没,JBOSS TOLL插件我全装了,汗.如图:


5.开始写代码,我都是写的main方法.注意把D:\software\jboss-as-7.1.3.Final\bin\client下的jar包弄上.

第一个是客户端的消息生产者:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package org.credo.jms;
 
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.Properties;
 
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
 
/**
 * <p>Description:JMS客户端消息生产者 </p>
 */
public class JMSProducer {
    private static final Logger log = Logger.getLogger(JMSProducer.class.getName());
 
    private static final String DEFAULT_MESSAGE = "the 4 message!";
    // xml文件272行
    private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
    // xml文件293行,初次找JBOSS的JNDI太不容易了
    private static final String DEFAULT_DESTINATION = "jms/queue/test";
    private static final String DEFAULT_MESSAGE_COUNT = "1";
 
    private static final String DEFAULT_USERNAME = "lion";
    private static final String DEFAULT_PASSWORD = "123456";
    private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
    private static final String PROVIDER_URL = "remote://localhost:4447";
 
    public static void main(String[] args) throws Exception {
        Context context=null;
        Connection connection=null;
        try {
            // 设置上下文的JNDI查找
            log.info("设置JNDI访问环境信息也就是设置应用服务器的上下文信息!");
            final Properties env = new Properties();
            env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);// 该KEY的值为初始化Context的工厂类,JNDI驱动的类名
            env.put(Context.PROVIDER_URL,  PROVIDER_URL);// 该KEY的值为Context服务提供者的URL.命名服务提供者的URL
            env.put(Context.SECURITY_PRINCIPAL, DEFAULT_USERNAME);
            env.put(Context.SECURITY_CREDENTIALS, DEFAULT_PASSWORD);//应用用户的登录名,密码.
            // 获取到InitialContext对象.
            context = new InitialContext(env);
            log.info("初始化上下文,'JNDI驱动类名','服务提供者URL','应用用户的账户','密码'完毕.");
 
            log.info("获取连接工厂!");
            ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup(DEFAULT_CONNECTION_FACTORY);
            log.info("获取目的地!");
            Destination destination = (Destination) context.lookup(DEFAULT_DESTINATION);
 
            // 创建JMS连接、会话、生产者和消费者
            connection = connectionFactory.createConnection(DEFAULT_USERNAME, DEFAULT_PASSWORD);
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer producer = session.createProducer(destination);
            connection.start();
 
            int count = Integer.parseInt(DEFAULT_MESSAGE_COUNT);
            // 发送特定数目的消息
            TextMessage message = null;
            for (int i = 0; i < count; i++) {
                message = session.createTextMessage(DEFAULT_MESSAGE);
                producer.send(message);
                log.info("message:"+message);
                log.info("message:"+DEFAULT_MESSAGE);
            }
            // 等待30秒退出
            CountDownLatch latch = new CountDownLatch(1);
            latch.await(30, TimeUnit.SECONDS);
             
        } catch (Exception e) {
            log.severe(e.getMessage());
            throw e;
        } finally {
            if (context != null) {
                context.close();
            }
            // 关闭连接负责会话,生产商和消费者
            if (connection != null) {
                connection.close();
            }
        }
    }
}

每次运行后如图:


JBOSS端如图所示:


每次向JBOSS的Queue发送消息成功的话,上面红圈的数字都会+1.

如果电脑重启,JBOSS重启,消息仍然是存在的.如果被接收了的话,消息就是消失.

2.客户端消费者代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package org.credo.jms;
 
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
 
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
 
public class JMSConsumer {
    private static final Logger log = Logger.getLogger(JMSConsumer.class.getName());
 
    private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
    private static final String DEFAULT_DESTINATION = "jms/queue/test";
    private static final String DEFAULT_USERNAME = "lion";
    private static final String DEFAULT_PASSWORD = "123456";
    private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
    private static final String PROVIDER_URL = "remote://localhost:4447";
 
    public static void main(String[] args) throws Exception {
 
        ConnectionFactory connectionFactory = null;
        Connection connection = null;
        Session session = null;
        MessageConsumer consumer = null;
        Destination destination = null;
        TextMessage message = null;
        Context context = null;
 
        try {
            final Properties env = new Properties();
            env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
            env.put(Context.PROVIDER_URL, PROVIDER_URL);
            env.put(Context.SECURITY_PRINCIPAL, DEFAULT_USERNAME);
            env.put(Context.SECURITY_CREDENTIALS, DEFAULT_PASSWORD);
            context = new InitialContext(env);
 
            connectionFactory = (ConnectionFactory) context.lookup(DEFAULT_CONNECTION_FACTORY);
            destination = (Destination) context.lookup(DEFAULT_DESTINATION);
 
            connection = connectionFactory.createConnection(DEFAULT_USERNAME, DEFAULT_PASSWORD);
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            consumer = session.createConsumer(destination);
            connection.start();
 
            // 等待30秒退出
            CountDownLatch latch = new CountDownLatch(1);
            while (message == null) {
                log.info("开始从JBOSS端接收信息-----");
                message = (TextMessage) consumer.receive(5000);
                latch.await(1, TimeUnit.SECONDS);
            }
            log.info("接收到的消息的内容:" + message.getText());
        } catch (Exception e) {
            log.severe(e.getMessage());
            throw e;
        } finally {
            if (context != null) {
                context.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
}

 

 

 

然后继续看JBOSS的控制台,可以发现消息减少了一条.

参考自:http://lym6520.iteye.com/blog/1600630

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics