首页 > 代码库 > wildfly 实践5 ---分布式服务中的JMS服务访问
wildfly 实践5 ---分布式服务中的JMS服务访问
实践条件与目标:
1. 分布式服务中主从服务相关配置
2. 从服务中主要代码片段展示
3. 此次使用wildfly10,因为其默认的jms服务是activemq.
步骤:
- 主服务配置中使用standalone-full.xml启动,其自带activemq模块。
- 使用adduser增加应用程序用户名称为guest,密码为guest,角色为guest,增加应用程序用户ejbuser,密码123。
- 配置文件中找到activemq模块,并修改配置如下:
<subsystem xmlns="urn:jboss:domain:messaging-activemq:1.0"> <server name="default"> <security-setting name="#"> <role name="guest" delete-non-durable-queue="true" create-non-durable-queue="true" consume="true" send="true"/> </security-setting> <address-setting name="#" message-counter-history-day-limit="10" page-size-bytes="2097152" max-size-bytes="10485760" expiry-address="jms.queue.ExpiryQueue" dead-letter-address="jms.queue.DLQ"/> <http-connector name="http-connector" endpoint="http-acceptor" socket-binding="http"/> <http-connector name="http-connector-throughput" endpoint="http-acceptor-throughput" socket-binding="http"> <param name="batch-delay" value="50"/> </http-connector> <in-vm-connector name="in-vm" server-id="0"/> <http-acceptor name="http-acceptor" http-listener="default"/> <http-acceptor name="http-acceptor-throughput" http-listener="default"> <param name="batch-delay" value="50"/> <param name="direct-deliver" value="false"/> </http-acceptor> <in-vm-acceptor name="in-vm" server-id="0"/> <jms-queue name="ExpiryQueue" entries="java:/jms/queue/ExpiryQueue"/> <jms-queue name="DLQ" entries="java:/jms/queue/DLQ"/> <jms-queue name="ticketqueue" entries="java:jboss/exported/jms/queue/ticketqueue"/> <connection-factory name="InVmConnectionFactory" entries="java:/ConnectionFactory" connectors="in-vm"/> <connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector"/> <pooled-connection-factory name="activemq-ra" transaction="xa" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory" connectors="in-vm"/> </server> </subsystem>
注意:我们这里创建的ticketqueue,必须使用java:jboss/exported/格式的jndi,否则外部机器无法访问
- 编写ticketqueue的MDB,并部署到主服务器中,主要代码如下:
@MessageDriven(name = "TicketBooked", activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "http://www.mamicode.com/javax.jms.Queue"), @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "http://www.mamicode.com/java:jboss/jms/queue/ticketqueue") }) public class TicketBookedMsg implements MessageListener { private final static Logger logger = Logger.getLogger(TicketBookedMsg.class); /** * Default constructor. */ public TicketBookedMsg() { // TODO Auto-generated constructor stub } /** * @see MessageListener#onMessage(Message) */ public void onMessage(Message message) { // TODO Auto-generated method stub try { final String text = message.getBody(String.class); logger.info("Received message " + text); } catch (JMSException ex) { logger.error("onmsg error" + ex.toString()); } }
- 从服务器可以使用standalone.xml启动,即默认的启动配置,在此配置文件中修改此处:
<subsystem xmlns="urn:jboss:domain:naming:2.0"> <bindings> <external-context name="java:global/JmsServer" module="org.jboss.as.naming" class="javax.naming.InitialContext" cache="true"> <environment> <property name="java.naming.factory.initial" value="org.jboss.naming.remote.client.InitialContextFactory"/> <property name="java.naming.provider.url" value="http-remoting://192.168.50.123:8080"/> <property name="java.naming.security.principal" value="ejbuser"/> <property name="java.naming.security.credentials" value="123"/> </environment> </external-context> </bindings> <remote-naming/> </subsystem>
注:此配置增加了一个外部context上下文环境,即访问远程主机jndi的context.
- 编写从主机相关服务代码,片段如下:
Context jmsctx = (InitialContext)context.lookup("java:global/JmsServer"); Destination queue = (Queue)jmsctx .lookup("jms/queue/ticketqueue"); ActiveMQConnectionFactory cf=(ActiveMQConnectionFactory)jmsctx .lookup("jms/RemoteConnectionFactory"); cf.setCallTimeout(3000); JMSContext jmsCtx=cf.createContext("guest", "guest"); jmsCtx.createProducer().send(queue, "ui started");
- 将写有此代码的服务部署到从服务器中,并调用。
- 在主服务器中显示如下:
09:37:50,032 INFO [com.sanjian.handmsg.TicketBookedMsg] (Thread-96 (ActiveMQ-client-global-threads-30092228)) Received message ui started
- 致此,wildfly10中分布式服务的jms通信主要框架已实践完成。
wildfly 实践5 ---分布式服务中的JMS服务访问
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。