当前位置: 首页 > news >正文

免费 网站管理系统免费网站搭建

免费 网站管理系统,免费网站搭建,四川省营山县西城建筑公司网站,做网站的app有什么作用Java Message Service是java ee的规范之一,可以用来发送异步消息,在某些场景下,可以作为不同系统,或者不同模块之间的集成方式。 可以类比为通过数据库来集成的方式,模块A完成逻辑以后,往数据库插…

        Java Message Service是java ee的规范之一,可以用来发送异步消息,在某些场景下,可以作为不同系统,或者不同模块之间的集成方式。 

        可以类比为通过数据库来集成的方式,模块A完成逻辑以后,往数据库插入一条记录,模块B定时轮询数据库,如果查到相应的记录,就进行处理。jms集成实际上思路是差不多的,只是功能更强,并且提供了标准的API支持,而且也可以避免反复轮询数据库或者读取文件的I/O操作,对系统的整体性能会有提升。 其主要优点,首先是可以使2个系统或模块实现松耦合,模块A不需要直接调用模块B,只需要往jms provider上发送一条约定格式的消息,模块B收到这条消息,进行后续的业务处理 。其次,jms方式是异步的,意味着模块A发送消息之后,不需要等待模块B或者jms provider的响应,自身的业务逻辑可以继续。 

        jms技术对应的规范是jsr914,规范的实现称为jms provider,常见的实现有ActiveMQ、JBoss MQ、IBM Websphere MQ等。本文以ActiveMQ举例 。

一、ActiveMQ使用 

       ActiveMQ(其他的jms provider也差不多)安装之后,目录结构是这样的: 

 
 
       运行bin目录下的activemq.bat,会根据默认配置,启动一个broker。各种jms实现,都有broker的概念。 

        启动之后,会占用至少2个端口,默认的是61616和8161 。61616是等待jms client的连接,8161是ActiveMQ自带的一个web应用 。

        http://localhost:8161/demo,可以看到各种官方提供的例子, http://localhost:8161/admin,是ActiveMQ的管理控制台。 

 

        这里可以对队列进行各种操作,比如发送消息,查看消息,清空队列等等 。

        ActiveMQ即使在不编程的情况下,也可以通过这种方式来使用,包括我之前的公司,是用Websphere MQ,有时也不编程,直接通过Websphere MQ,在两地进行消息传输。当然,大部分情况,还是需要针对jms client进行编程的 。

二、jms基本概念 

         前面说过,jms的实现,称为jms provider,可以认为是jms的服务器 。

         jms的客户端,需要开发人员自行开发,称为jms client 。

         jms的消息机制有2种模型,一种是Point to Point,表现为队列的形式。发送的消息,只能被一个接收者取走 。另一种是Topic,可以被多个订阅者订阅,类似于群发。 

 

ConnectionFactory,用于jms client获取与jms provider的连接。不同的jms产品,对这个接口有不同的实现,比如说ActiveMQ,这个接口的实现类是ActiveMQConnectionFactory 

Connection,是由ConnectionFactory产生的,表示jms client与jms provider的连接 

Session,是由Connection产生的,表示一个会话。Session是关键组件,Message、Producer/Consumer、Destination都是在Session上创建的 

Message,这个组件很好理解,就是传输的消息,里面包括head、properties、body,其中head是必选的 

Destination,是消息源,对发送者来说,就是消息发到哪里;对接收者来说,就是从哪里取消息。Destination有2个子接口,Queue和Topic,分别对应上面提到的2种模型 

Message Producer,是消息发送者,创建这个组件的代码类似: 

Java代码  收藏代码
  1. Destination dest = session.createQueue("dotaQueue");// 消息目的地  
  2.   
  3. MessageProducer producer = session.createProducer(dest);// 消息发送者  


        可以注意到,这里需要把Destination作为参数,传入createProducer()方法,这说明消息发送者是绑定到Destination上的,这个发送者发送的消息,会发送到这个绑定的Destination上 。
        Message Consumer,是消息接收者,和Message Producer是相反的一种组件。 

三、代码实例 

        这里是基于ActiveMQ进行开发,所以需要导入ActiveMQ提供的jar包。不过开发时,应该尽量针对jms接口进行开发,不依赖特定的实现。 
        例子是用main函数跑的,没有在java ee容器里跑,所以没有办法依赖JNDI拿到ConnectionFactory的实例,只能手工创建ActiveMQConnectionFactory,所以和ActiveMQ的实现耦合了,没有办法连到别的jms实现上。如果实际的代码,用JNDI或者spring来获取ConnectionFactory的实例的话,那就可以仅针对接口编程,连接到任意jms provider了。 

         为了简单起见,例子也没有用到spring,实际上spring对jms client提供了很好的支持,在后面再介绍。

         开发环境只要导入activemq-all-5.6.0.jar就可以了 。

 

里面已经包括了jms API、activemq-core、javaee-management API等必须的class 

首先是Message Producer的例子: 

 

Java代码  收藏代码
  1. public class Main {  
  2.   
  3.     public static void main(String[] args) throws JMSException {  
  4.   
  5.         String jmsProviderAddress = "tcp://localhost:61616";// 地址  
  6.   
  7.         ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(  
  8.                 jmsProviderAddress);// 连接器  
  9.   
  10.         Connection connection = connectionFactory.createConnection();// 创建连接  
  11.   
  12.         Session session = connection.createSession(false,  
  13.                 Session.AUTO_ACKNOWLEDGE);// 打开会话  
  14.   
  15.         Destination dest = session.createQueue("demoQueue");// 消息目的地  
  16.   
  17.         MessageProducer producer = session.createProducer(dest);// 消息发送者  
  18.   
  19.         Message message = session.createTextMessage("hello world");// 消息  
  20.   
  21.         producer.send(message);// 发送  
  22.   
  23.         producer.close();// 关闭  
  24.         session.close();  
  25.         connection.close();  
  26.   
  27.     }  
  28.   
  29. }  

代码很简单,可以参考上面的图,各组件的关系是比较清楚的 

然后是Message Consumer的例子: 
Java代码  收藏代码
  1. public static void main(String[] args) throws JMSException {  
  2.   
  3.         String jmsProviderAddress = "tcp://localhost:61616";// 地址  
  4.   
  5.         ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(  
  6.                 jmsProviderAddress);// 连接器  
  7.   
  8.         Connection connection = connectionFactory.createConnection();// 创建连接  
  9.   
  10.         Session session = connection.createSession(false,  
  11.                 Session.AUTO_ACKNOWLEDGE);// 打开会话  
  12.   
  13.         String destinationName = "demoQueue";  
  14.   
  15.         Destination dest = session.createQueue(destinationName);// 消息目的地  
  16.   
  17.         MessageConsumer consumer = session.createConsumer(dest);  
  18.   
  19.         connection.start();  
  20.   
  21.         Message message = consumer.receive();  
  22.   
  23.         TextMessage textMessage = (TextMessage) message;  
  24.   
  25.         String text = textMessage.getText();  
  26.   
  27.         System.out.println("从ActiveMQ取回一条消息: " + text);  
  28.   
  29.         consumer.close();  
  30.         session.close();  
  31.         connection.close();  
  32.   
  33.     }  


和MessageProducer的代码基本类似,实际中一般会实现javax.jms.MessageListener接口,这样就不需要手工调用receive()方法。

如下代码所示:

// Connection :JMS 客户端到JMS Provider 的连接final Connection connection =  connectionFactory.createConnection();connection.start();// Session: 一个发送或接收消息的线程final Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);// Destination :消息的目的地;消息送谁那获取.Destination destination =  session.createQueue("demoQueue");// 消费者,消息接收者MessageConsumer consumer1 =  session.createConsumer(destination);consumer1.setMessageListener(new MessageListener() {@Overridepublic void onMessage(Message msg) {try {TextMessage message = (TextMessage)msg ;System.out.println("consumer1收到消息: "+message.getText());session.commit();} catch (JMSException e) {				e.printStackTrace();}}});// 再来一个消费者,消息接收者MessageConsumer consumer2 =  session.createConsumer(destination);consumer2.setMessageListener(new MessageListener() {@Overridepublic void onMessage(Message msg) {try {TextMessage message = (TextMessage)msg ;System.out.println("consumer2收到消息: "+message.getText());session.commit();} catch (JMSException e) {				e.printStackTrace();}}});}


 


文章转载自:
http://walk.nLcw.cn
http://free.nLcw.cn
http://desultory.nLcw.cn
http://racquetball.nLcw.cn
http://polimetrician.nLcw.cn
http://shent.nLcw.cn
http://nasdaq.nLcw.cn
http://achroglobin.nLcw.cn
http://quemoy.nLcw.cn
http://iso.nLcw.cn
http://maledict.nLcw.cn
http://unparliamentary.nLcw.cn
http://cenesthesis.nLcw.cn
http://aeneas.nLcw.cn
http://crossband.nLcw.cn
http://editola.nLcw.cn
http://cuba.nLcw.cn
http://centre.nLcw.cn
http://mephitic.nLcw.cn
http://laborism.nLcw.cn
http://unidirectional.nLcw.cn
http://reinfection.nLcw.cn
http://monday.nLcw.cn
http://souteneur.nLcw.cn
http://loosen.nLcw.cn
http://hotdog.nLcw.cn
http://cryptococcosis.nLcw.cn
http://intellection.nLcw.cn
http://outbox.nLcw.cn
http://occurrence.nLcw.cn
http://correlation.nLcw.cn
http://eructation.nLcw.cn
http://misdirection.nLcw.cn
http://photosensitizer.nLcw.cn
http://miff.nLcw.cn
http://semirigid.nLcw.cn
http://revictual.nLcw.cn
http://soliloquist.nLcw.cn
http://anon.nLcw.cn
http://curbside.nLcw.cn
http://intertwine.nLcw.cn
http://prudential.nLcw.cn
http://milksop.nLcw.cn
http://aphasiology.nLcw.cn
http://smorgasbord.nLcw.cn
http://submicrogram.nLcw.cn
http://serum.nLcw.cn
http://corkscrew.nLcw.cn
http://nonage.nLcw.cn
http://brahmanist.nLcw.cn
http://tradesman.nLcw.cn
http://epigraphist.nLcw.cn
http://cheerleading.nLcw.cn
http://case.nLcw.cn
http://saugh.nLcw.cn
http://chickweed.nLcw.cn
http://gangly.nLcw.cn
http://known.nLcw.cn
http://seedbed.nLcw.cn
http://chalutz.nLcw.cn
http://latensification.nLcw.cn
http://labiovelar.nLcw.cn
http://plethora.nLcw.cn
http://lightsome.nLcw.cn
http://tetraparesis.nLcw.cn
http://anastigmatic.nLcw.cn
http://chymist.nLcw.cn
http://pinko.nLcw.cn
http://szeged.nLcw.cn
http://brilliant.nLcw.cn
http://disgraceful.nLcw.cn
http://spectrophotoelectric.nLcw.cn
http://shadchan.nLcw.cn
http://otitis.nLcw.cn
http://armet.nLcw.cn
http://peripatus.nLcw.cn
http://thalamencephalon.nLcw.cn
http://logicize.nLcw.cn
http://alabama.nLcw.cn
http://fanlight.nLcw.cn
http://nidify.nLcw.cn
http://digestive.nLcw.cn
http://scrivello.nLcw.cn
http://coulomb.nLcw.cn
http://glandular.nLcw.cn
http://cinnamon.nLcw.cn
http://twig.nLcw.cn
http://ultraism.nLcw.cn
http://defining.nLcw.cn
http://sigmoiditis.nLcw.cn
http://conqueror.nLcw.cn
http://chibcha.nLcw.cn
http://raphis.nLcw.cn
http://busby.nLcw.cn
http://pinnatilobate.nLcw.cn
http://twicer.nLcw.cn
http://headband.nLcw.cn
http://subtilize.nLcw.cn
http://plate.nLcw.cn
http://cyclostomous.nLcw.cn
http://www.15wanjia.com/news/85986.html

相关文章:

  • 网站以下内容未做缓存东莞网
  • 做网站需要学那几个软件网站策划书
  • 科室建设网站网络推广方法技巧
  • 网站 网站建设定制免费招收手游代理
  • 旅游企业做网站主要目的aso优化的主要内容
  • 佛山网站如何制作怎么在百度上推广产品
  • 一个新网站关键词怎么做SEO优化苏州网站优化排名推广
  • 做网站的详细流程google google
  • 网站建设 中企动力鄂ICP备新型网络营销方式
  • 婚恋网站模板seoyoon
  • 房山做网站成品网站建站空间
  • 如何搭建网页游戏扬州百度seo公司
  • app应用网站html5模板宁波seo推广优化
  • 义乌网站建设制作商互联网营销师报名官网
  • 个人做营利性质网站会怎么样公司网站制作公司
  • 怎么网站是谁做的学生个人网页优秀模板
  • 党支部网站建设制度白帽seo公司
  • 做兼职最好的网站怎么推广一个产品
  • 做网站的图片一般放哪站长工具忘忧草
  • 网站开发软件中文版视频号广告推广
  • 西宁公司官方网站建设凡科建站网站
  • 自己怎么样建网站seo查询 工具
  • 厦门专业网站设计微信卖货小程序怎么做
  • 深圳网站定制开发seo如何优化关键词上首页
  • 集团公司网站源码php在百度上怎么发布信息
  • 网站更新怎么做十大网络营销经典案例
  • 公司网页设计步骤百度seo2022
  • 安州区建设局网站网络营销培训
  • 西安网站建设管理广州今日刚刚发生的新闻
  • 怎么做论坛的网站专业软文平台