开发运行环境:NetBeans7.3.1。
这里使用的是JMS 2.0的特性,关于JMS 2.0的介绍,请参考《JavaEE7 十大新特性》。
这里使用的是JMS 2.0的特性,关于JMS 2.0的介绍,请参考《JavaEE7 十大新特性》。
1. SendPointsBean.java
SendPointsBean.java用来模拟发送观影积分,消息会发送一个Queue中。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.glassfish.movieplex7.points;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.jms.JMSContext;
import javax.jms.Queue;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
/**
*
* @author pmma
*/
@Named
@RequestScoped
public class SendPointsBean {
@NotNull
@Pattern(regexp = "^\\d{2},\\d{2}",
message = "Message format must be 2 digits, comma, 2 digits, e.g.12 ,12")
private String message;
@Inject
JMSContext context;
@Resource(lookup = "java:global/jms/pointsQueue")
Queue pointsQueue;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void sendMessage() {
System.out.println("Sending message: " + message);
context.createProducer().send(pointsQueue, message);
}
}
说明:
(1)这里使用了@Inject,依赖注入了JMSContext,JMSContext是JMS 2.0新增的Interface。
(2)Java EE 7 会创建一个默认的JMS Connection Factory:java:comp/DefaultJMSConnectionFactory。
(3)发送信息使用 context.createProducer().send(pointsQueue, message);
2. ReceivePointsBean.java
ReceivePointsBean.java用来从Queue中接收消息。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.glassfish.movieplex7.points;
import java.util.Enumeration;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.jms.JMSContext;
import javax.jms.JMSDestinationDefinition;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
/**
*
* @author pmma
*/
@JMSDestinationDefinition(name = "java:global/jms/pointsQueue",
interfaceName = "javax.jms.Queue")
@Named
@RequestScoped
public class ReceivePointsBean {
@Inject
JMSContext context;
@Resource(lookup = "java:global/jms/pointsQueue")
Queue pointsQueue;
public String receiveMessage() {
String message =
context.createConsumer(pointsQueue).receiveBody(String.class);
System.out.println("Received message: " + message);
return message;
}
public int getQueueSize() {
int count = 0;
try {
QueueBrowser browser = context.createBrowser(pointsQueue);
Enumeration elems = browser.getEnumeration();
while (elems.hasMoreElements()) {
elems.nextElement();
count++;
}
} catch (JMSException ex) {
ex.printStackTrace();
}
return count;
}
}
说明:
(1)接收信息使用 createConsumer或createDurableConsumer,接收方式可以是同步的,也可以是异步的。
(2)使用context.createBrowser(pointsQueue);可以查看Queue的大小,即消息的数量。
3. points.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<body>
<ui:composition template="./../WEB-INF/template.xhtml">
<ui:define name="content">
<h1>Points</h1>
<h:form>
Queue size:
<h:outputText value="#{receivePointsBean.queueSize}"/><p/>
<h:inputText value="#{sendPointsBean.message}"/>
<h:commandButton value="Send Message" action="points"
actionListener="#{sendPointsBean.sendMessage()}"/>
</h:form>
<h:form>
<h:commandButton value="Receive Message" action="points"
actionListener="#{receivePointsBean.receiveMessage()}"/>
</h:form>
</ui:define>
</ui:composition>
</body>
</html>
Project 下载:8. movieplex7(JMS).7z
没有评论:
发表评论