1
添加Destination
修改域deploy下messaging文件夹下的destinations-service.xml文件
添加一个mbean:
<mbean code="org.jboss.jms.server.destination.QueueService"
name="jboss.messaging.destination:service=Queue,name=OrderBillingQueue"
xmbean-dd="xmdesc/Queue-xmbean.xml">
<attribute name="JNDIName">queue/OrderBillingQueue</attribute>
<depends optional-attribute-name="ServerPeer">
jboss.messaging:service=ServerPeer
</depends>
<depends>jboss.messaging:service=PostOffice</depends>
<attribute name="RedeliveryDelay">10000</attribute>
<attribute name="MaxDeliveryAttempts">3</attribute>
</mbean>
2 添加MDB:
可根据自己的情况修改下面的代码
package ejb3inaction.example.buslogic;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
@MessageDriven(activationConfig={
@ActivationConfigProperty(propertyName="destinationType",
propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName="destination",propertyValue="queue/OrderBillingQueue")
}
)
public class OrderBillingMDB implements MessageListener {
public void onMessage(Message message) {
ObjectMessage objectMsg = (ObjectMessage)message ;
try {
Order order = (Order)objectMsg.getObject() ;
order.setStatus(OrderStatus.COMPLETE) ;
System.out.printf("order is finished." ) ;
} catch (JMSException e) {
e.printStackTrace();
}
}
}
3 测试代码
视自己的情况修改:
try {
Context context = new InitialContext() ;
connectionFactory = (ConnectionFactory)context.lookup("/ConnectionFactory") ;
destination = (Destination)context.lookup("queue/OrderBillingQueue") ;
} catch (NamingException e1) {
e1.printStackTrace();
}
Order order = new Order() ;
order.setBidderId(bidderId) ;
order.setItems(items) ;
order.setShippingInfo(shippingInfo) ;
order.setBidderId(bidderId) ;
order.setStatus(OrderStatus.NEW) ;
System.out.println("调用jms从bill中扣掉所需……,设置订单状态……") ;
try {
Connection conn = connectionFactory.createConnection() ;
Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE) ;
ObjectMessage message = session.createObjectMessage() ;
message.setObject(order) ;
MessageProducer producer = session.createProducer(destination) ;
producer.send(message) ;
session.commit() ;
producer.close() ;
session.close() ;
conn.close() ;
} catch (JMSException e) {
e.printStackTrace();
}
4 如果context取到的东西为空,则在src下添加jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099