JBoss QuickStart之Helloworld

2023-05-24,,

下载Jboss, quickstart, 按照quickstart说明, mvn clean install.

由于ssl handshake问题(应该是网络连接不稳定), 写了一个脚本不停地尝试build, 连续build了22次才成功.

启动jboss,注意jboss与quickstart的version要一致. 运行: mvn clean install jboss-as:deploy

注:如何查看Jboss version: standalone -v

HelloWorld

@WebServlet 上来就看web.xml,竟然啥也没有,原来servlet3.0定义了新的annotation. 关于用法请参见Servlet 3.0 Spec

这里用到的例子: @WebServlet("/HelloWorld") 等价于web.xml中定义的<servlet>以及<servlet-mapping>, 告诉container,加载这个url与class之间的映射。

@Inject 又一个新东西,在Servlet3.0里面搜索,啥也没有,原来是J2EE的CDI(Contexts and Dependency Injection 上下文依赖注入),如此这般。注解触角越深越多。

是J2EE哪个版本里引入的新特性呢?看来需要看一下J2EE Spec。。。请看官网link, 是J2EE6.0里的JSR330 Dependency Injection for Java 1.0

HelloWorld-JMS

JMS, 它需要JNDI配置,远程调用,所以需要:Jndi name。还需要添加application user,用add-user.bat添加,设置guest role即可。

运行: mvn clean compile exec:java (学到一个mvn 执行设置系统变量的小技巧,可以参见目录中pom.xml)

看了一下原代码,

先look up factory, 然后look up JMS queue, send and receive, over!

            // Perform the JNDI lookups
String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryString);
log.info("Found connection factory \"" + connectionFactoryString + "\" in JNDI"); String destinationString = System.getProperty("destination", DEFAULT_DESTINATION);
log.info("Attempting to acquire destination \"" + destinationString + "\"");
destination = (Destination) context.lookup(destinationString);
log.info("Found destination \"" + destinationString + "\" in JNDI"); // Create the JMS connection, session, producer, and consumer
connection = connectionFactory.createConnection(System.getProperty("username", DEFAULT_USERNAME), System.getProperty("password", DEFAULT_PASSWORD));
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination);
consumer = session.createConsumer(destination);
connection.start(); int count = Integer.parseInt(System.getProperty("message.count", DEFAULT_MESSAGE_COUNT));
String content = System.getProperty("message.content", DEFAULT_MESSAGE); log.info("Sending " + count + " messages with content: " + content); // Send the specified number of messages
for (int i = 0; i < count; i++) {
message = session.createTextMessage(content);
producer.send(message);
} // Then receive the same number of messages that were sent
for (int i = 0; i < count; i++) {
message = (TextMessage) consumer.receive(5000);
log.info("Received message with content " + message.getText());
}

HelloWorld-MBean

有点被MBean搞晕了,到处都是注解,nd。

首先对MBean的认识有限,就是符合JMX框架规范的JavaBean,用来管理资源的,这时候我就想到为何网管不采用部署Mbean的方式呢,说白了就是get,Set一些参数,关键上层还有能对SNMP的支持。下面是我碰到的一些注解,这里面涉及到CDI概念(这里是一篇关于CDI的文章),还有EJB:

@PostConstruct  <--J2SE支持的注解(javax扩展包), The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization

@PreDestroy<--J2SE支持的注解(javax扩展包), The PreDestroy annotation is used on methods as a callback notification to signal that the instance is in the process of being removed by the container。

@Singleton <-- 分为 javax.ejb.Singleton, javax.inject.Singleton, 本例中为前者ejb。表明为单例,不知道该怎样使用。

@Startup   <-- javax.ejb.Startup, Mark a singleton bean for eager initialization during the application startup sequence. 何谓eager?应该就是非lazy加载方式。

@MXBean  <--JMX的annotation

看到这个代码很迷惑,注解竟然出现在参数当中,查了文档后,注解也支持parameter。但这个beforeBeanDiscovery又是个啥东西,不懂得太多了!

void beforeBeanDiscovery(@Observes BeforeBeanDiscovery beforeBeanDiscovery, BeanManager beanManager) {
        setBeanManager(beanManager);
 }

上面的代码涉及到CDI容器,容器事件监听器,@Observes注解(本例中注解表明 beforeBeanDiscovery是监听器方法,监听的事件的类型为 beforeBeanDiscovery)。

HelloWorld-MDB

Message Driver Bean, 10年前用过,现在又回来了,现在不用在部署描述符中写配置,只需在你的代码中写Annotation。(其实我目前很憎恶这个annotation,隐藏错误,隐藏细节,感觉很不痛快,对编程是一种破坏,给人一种支离破碎的感觉。)

@MessageDriven(name = "HelloWorldQueueMDB", activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/HELLOWORLDMDBQueue"),
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })

JBoss QuickStart之Helloworld的相关教程结束。

《JBoss QuickStart之Helloworld.doc》

下载本文的Word格式文档,以方便收藏与打印。