1. 首页
  2. 编程语言
  3. 其他
  4. ActiveMQ in Action

ActiveMQ in Action

上传者: 2019-09-14 01:08:14上传 PDF文件 563.81KB 热度 15次
ActiveMQ in Action的开发文档1.2.3优先级可以使用消息优先级来指示 JMs provider首先提交紧急的消息。优先级分10个级别,从0(最低)到9(最高)。如果不指定优先级,默认级别是4。需要注意的是, JMs provider并不一定保证按照优先级的顺序提交消息。1.2.4消息过期可以设置消息在一定时间后过期,默认是永不过期2.5临时目的地可以通过会话上的 createTemporaryQueue方法和 createTemporary Topic方法米创建临时目的地。它们的存在时间只限于创建它们的连接所保持的时间。只有创建该临时日的地的连接上的消息消费者才能够从临时日的地中提取消息。1.2.6持久订阅首先消息生产者必须使用 PERSISTENT提交消息。客户可以通过会话上的createDurableSubscriber方法来创建一个持久订阅,该方法的第一个参数必须是一个 topic。第二个参数是订阅的名称。JMS provider会存储发布到持久订对应的 topic上的消息。如果最初仓建持久订阅的客户或者任何其亡客户使用相同的连接工厂和连接的客户ID、相同的主题和相同的订阅名再次调用会话|的 createDurablesubscriber方法,那么该持久订阅就会被激活。 JMS provider会象客户发送客户处于非激活状态时所发布的消总。持久订阅在某个时刻只能有一个激活的订阅者。持久订阅在创建之后会一直保留,直到应用程序调用会话上的 unsubscribe方法。1.2.7本地事务在一个JMS客户端,可以使用本地事务来组合消息的发送和接收。JMSSession接口提供了 COMml t和 rollback方法。事务提交意味着生产的所有消息被发送,消费的所冇消息被确认;事务回滚意味着生产的所有消息被销毁,消费的所冇消息被恢复并重新提交,除非它们已经过期。事务性的会话总是牵涉刭事务处理中, commi t或 rollback方法一日被调川,个事务就结束了,而另一个事务被开始。关闭事务性会话将回滚其屮的事务。需要注意的是,如果使用请求/回复机制,即发送一个消总,同时希望在同一个事务中等待接收该消息的回复,那么程序将被挂起,因为知道事务提交,发送操作才会真正执行。需要注意的还有一个,消息的生产和消费不能包含在同一个事务中1.3JMS规范的变迁JMS的最新版本的是1.1。它和同1.0.2版本之间最大的差别是,JMS1.1通过统一的消息传递域简化了消息传递。这不仅简化了 JMS API,也有利于开发人员灵活选择消息传递域,同时也有助于程序的重用和维护。以下是不同消息传递域的相应接口JMS公共点对点域发布/订阅域ConnectionFactory Queue ConnectionFactory TopicConnection FactoryConnectionQueue ConnectionTopicConnectionDestinationQueueTopicSessionQueuesessionTopicSessionMessageProducer QueueSenderTopicPublisheMessage Consumer QueueReceiverTopicSubscriber2 ActiveMQ2. 1 Broker2. 1. 1 Running BrokerActiveMQ5.0的二进制发布包中bin日录中包含一个名为 activemq的脚本,直接运行这个脚本就可以启动一个 broker此外也可以通过 Broker ConfiguratiOn URI或 Broker xBean uri对 broker进行配置,以下是一些命令行参数的例子:ExampleDescriptionRuns a broker using thedefaultactivemqI xbean: activemq. xmI'asthe broker configurationfileRuns a broker using the filemyconfig xml as the brokerlactivemg xbean: my config. xmlconfiguration file that isocated in the classpathRuns a broker using the filebroker. xml as the brokeractivemq xbean: file:./conf/broker. xmlconfiguration file that islocaled in the relativeath./ conf/broker. xmRuns a broker using the filedcLlveilybroker 2. xi as the brokerxbean: file:C:/ActiveMQ/conf/broker2xml configuration file that islocated in the absoluteC: /ActiveMQ/conf/broker2xmRbroker with twoactivemq broker:(tcp: //localhost: 61616tcp://localhost:5000)?useJmx-truetransport connectors andJMX enabledRuns a broker with 1activemq broker:(tcp: //localhost: 61616network:tcp://localhost:5000)?persistent=ftransport connector and 1network connector withalsepersistence disabled2. 1.2 Embedded broker可以通过在应用程序中以编码的方式启动 broker,例如:Java代码1. Broker Service broker=new Broker Service2. broker. addConnector( tcp //localhost: 61616")3. broker. start o如果需要启动多个 broker,那么需要为 broker设置一个名字。例如:Java代码BrokerService broker new Broker Service2. broker. se LName(fred")3. broker. addConnector(tcp: //localhost: 616164. broker. start o如果希望在同一个JM内访问这个 broker,那么可以使用 VM Transpor tURI是:Vm:// brokerName。关于更多的 broker属性,可以参考 Apache的官方文档。此外,也可以通过 Brokerfactory来创建 broker,例如:Java代码1.Broker Service broker= BrokerFactory createBroker(new URI (someURD))somero的可选值如下URIExampleDescriptionschemexbean:xbean: activemq. xmlSearches the classpath for an XMLdocument with the given LRI(activemg. xml in this case) whichwill then be used as the XmlConfigurationLoads the given file (in thisfile: foo/bar/activemq xml example Too/bar/actively, xml)asthe Xml Configurationbroker: broker: tcp: //localhost: 61616Uses the broker Configuration URIto configure the broker当使用 XBean的配置方式的时候,需要指定一个xm1配置文件,例如Java代码1. Broker Service broker=BrokerFactory. createBroker(new URI("Xbean: com/test/activemg xm1" )使用 Spring的配置方式如下Xm1代码回1. 4.4.6.接下来运行JDK自带的 jconsole在运行了 jconsole后,它会弹出对话框来选择需要连接到的 agent。如果是在启动 broker的主机上运行 jconsole,那么 ActivemQ broker会出现在 jconsole的 Local标繚中。如果要连接到远程的broker,那么可以在 Advanced标签中指定 JMX URL,以下是一个连接到本机的JMX URL:ervice: jmx: rmi: ///jndi/rmi: //localhost: 1099/jmxrmi在 jconsole的 MBeans标签中,可以查看详细信息,也可以执行相应的operation。需要注意的是,在 jconsole连接到 broker的时候,并不需要输入用户名和密码,如果这存在潜在的安全问题,那么就需要为 JMX Connector配置密码保护(需要使用1.5以上版本的JDK)首先要禁止 ActiveMQ创建自己的 connector,例如:Xml代码1.management Context>Management Context createConnector= false"/>4. 5. 然后在 ActivemQ的conf目录下创建一个访问控制文件和密码文件,如下conf/jmx access:The torrole role has readonly accessThe controlrole role has readwrite accessmonilorrole readonlycontrolrole readwriteconf/jmx passwordThe moni torrole role has password abc123The controlrole role has password abcd1234monitorrole abcl23controlrole abcd1234然后修改 ActiveMQ的bin目录下 actived的启动脚木,查找包含" SUNJMX=的一行如下:REM set SUNJMX=-Dcom sun management. jmxremote port=1616Dcom. sun management. jmxremote authenticate=false-Dcom. sun management jmxremote ssl-false把它替换成set SUNJMX-DcOm, Surl. anagement. jmxremote port=1616Dcom sun management jmxremote authenticate=trueDcom. sun. management. jmxremote ssl=false-Dcom. sun. management. jmxremote. password file=%ACTIVEMQ BASE%/conf/jmxpassword-Dcom. sun management jmxremote access file=%ACTIVEMQ BASE%/conf/jmxaCcess最后重启 ActiveMQ和 jconsole,这时候需要强制 login。如果在启动activeI的过程中出现以下错误,那么需要为这个文件增加访问控制。 Windows平台上的具体解决方法请参考如下网址:http://java.suncom/j2se/1.5.0/docs/guide/management/security-windowshtmlError: Password file read access must be restrictedD: \apache-activem-. 0. 0\bin\./ conf/jmx password2.1.3.2 Web ConsoleWeb console被集成到了 ActiveMQ的二进制发布包中,因此缺省访问http://localhost:8161/admin即可访问Webconsole在配置文件屮,可以通过修改 inconnector的port属性来修改 Web console的缺省端口Xm1代码1.jettyxmins=hTtp://mortbay.com/schemas/jetty/1.0">〈 connectorsKnioConnector port=8161/>4. 6.;如果希望订阅所有 queue或者 topic上的 start/stop消息,那么可以订阅 ActiveMQ. Advisory. Consumer.>。org. apache. activemq. advisory. Advisory support类上有如下的 helpermethods,用来在程序中得到 advisory destination ob jects
下载地址
用户评论