activemq serviceMix集成学习

activemq:query --jmxlocal -QTopic=* --view EnqueueCount,DequeueCount

 osgi:install -s war:mvn:org.apache.activemq/activemq-web-console/5.7.0/war?Webapp-Context=activemqweb&Bundle-Classpath=.,WEB-INF/tags/form,WEB-INF/tags/jms

 activemq:query --jmxurl service:jmx:rmi:///jndi/rmi://localhost:1099/karaf-root --jmxuser xms --jmxpassword sms  -QTopic=* --view EnqueueCount,DequeueCount

ActiveMQ provides Karaf features which will help you integrate and use the broker in OSGi environment. For starters you need to add the features URL to Karaf. For version 5.8.0 you can do it like this:

karaf@root> features:chooseurl activemq 5.8.0

After that you should see newly added features

karaf@root> features:list
State         Version         Name                 Repository
[uninstalled] [5.8.0          ] activemq-broker               activemq-5.8.0         
[uninstalled] [5.8.0          ] activemq-http                 activemq-5.8.0         
[uninstalled] [5.8.0          ] activemq-camel                activemq-5.8.0         
[uninstalled] [5.8.0          ] activemq-web-console          activemq-5.8.0 

Installing and running the broker is as simple as installing activemq-broker feature, like

karaf@root> features:install activemq-broker

This will install and start the full broker (including the web console), just as if you started the standalone distribution.

Broker Configuration

Broker is configured using OSGi Config Admin mechanism and could be easily managed in Karaf. Configuration can be done by modifying${KARAF_BASE}/etc/org.apache.activemq.server-default.cfg file or respective config admin property. An example of the file looks like

broker-name=amq-broker
data=${karaf.data}/${broker-name}
config=${karaf.base}/etc/activemq.xml

Mandatory properties are listed in the following table

Property NameProperty Description
broker-nameName of the broker
configLocation of the XML configuration file

You can also use this file to set other properties which will replace placeholders in XML configuration file, as the ${data} property is used in this example.

Default XML configuration file is located in the ${KARAF_BASE}/etc/activemq.xml by default.

Web Console

Web Console is installed by default and can be reached at http://localhost:8181/activemqweb/

The configuration for the console is done in a similar fashion to the broker itself. Configuration is located in${KARAF_BASE}/etc/org.apache.activemq.webconsole.cfg and by default looks like

webconsole.jms.url=tcp://localhost:61616
webconsole.jmx.url=service:jmx:rmi:///jndi/rmi://localhost:1099/karaf-root
webconsole.jmx.user=karaf
webconsole.jmx.password=karaf

Optional: In order to use the ActiveMQ console with a broker configured with authentication, it is necessary to configure the username/password for JMS connection as well.

webconsole.jms.user=system
webconsole.jms.password=manager

Commands

After these simple steps you have all necessary libraries installed in the container. Also, now you have specific commands on your disposal that you can use to manage your broker:

  browse                Display selected messages in a specified destination
  bstat                 Displays useful broker statistics
  list                  Lists all available brokers in the specified JMX context
  purge                 Delete selected destination's messages that matches the message selector
  query                 Display selected broker component's attributes and statistics
Help on commands
To obtain some detailed help on a given command, you can run:
activemq:[command] --help 

Broker querying

Several commands are available to query the broker. To address local brokers, you need to use the --jmxlocal parameter.

The following command displays available brokers:

karaf@root> activemq:list --jmxlocal
BrokerName = mybroker

To have more detailed informations, run:

karaf@root> activemq:query --jmxlocal

It will display informations about the connectors, list of queues, etc...

You can also browse or purge queues using the activemq:browse and activemq:purge commands.


 
javax.naming.NoInitialContextException: Unable to determine caller's BundleContext
 
install -s mvn:org.apache.aries.jndi/org.apache.aries.jndi/1.0.0
install -s mvn:org.apache.aries.proxy/org.apache.aries.proxy/1.0.0
install -s mvn:org.apache.aries/org.apache.aries.util/1.1.0
 

Remote JMX Exceptions

Perhaps the most difficult aspect of getting a freshly written  JSR-160 ( Remote JMX) compliant application working is ensuring a proper  JMXServiceURL. In this blog entry, I cover some of the most common  JMXServiceURL-related exceptions one may run into when developing the client and server portions of a management and monitoring system using Remote JMX.  The  JMX 1.4 Specification combines the original JMX specification with the Remote JMX specification in a  single document (the JMX Remote API Specification is part III of  this document). This third part covers the required  RMI  connector as well as the optional JMXMP connector. In this blog entry, I'll be focusing on using the RMI connector with  Java SE 6  HotSpot  platform MBean server. Instead of using  JConsoleas a client, I'll use a simple custom client to illustrate some of the exceptions one may encounter when writing one's own client. I am using essentially the same code in this blog for examples that I previously used in the previous blog entry  Remote JMX: With and Without Spring and Proxies. I will not reproduce the code here, but it is sufficient to know that the JMX Connector server code generally establishes its  JMXServiceURL as service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi. Note that the protocol is RMI, the host is localhost, the port is 1099, and the URL string is "jmxrmi". Because the JMX client needs to use the same JMXServiceURL as provided by the server, I appreciate it when third-party JMX servers provide this to me. For example, both  GlassFish and the  JMX Web Services Connector  Reference Implementation  sample provide their  JMXServiceURL to the console when they are executed. When the  JMXServiceURL is known, it is relatively straightforward to write a client. The most significant lines of code for doing this are extracted from the code in the previously referenced blog and displayed next:
  1. final String objectNameStr = "dustin:type=status,name=remoteJMX";  
  2. final String jmxRmiStr =  
  3.    "service:jmx:rmi://localhost/jndi/rmi://localhost:1099/jmxrmi";  
  4. try  
  5. {  
  6.    final ObjectName objectName = new ObjectName(objectNameStr);  
  7.    final JMXServiceURL jmxUrl = new JMXServiceURL(jmxRmiStr);  
  8.    final JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxUrl);  
  9.    final MBeanServerConnection mbsc = jmxConnector.getMBeanServerConnection();  
As the code listing above demonstrates, it is fairly easy (and mostly boilerplate) to connect clients to a JMX Connector Server with a well-known  JMXServiceURL. However, it is not uncommon for new JMX developers to struggle to get the  JMXServiceURL appropriately set. In the remainder of this blog entry, I now look at some of the most common  JMXServiceURL-related exceptions one may run into. ERROR: IOException trying to connect to JMX Connector Server: Failed to retrieve RMIServer stub: javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:  java.net.ConnectException: Connection refused: connect] A possible cause of this exception includes no JMX Connector Server running on the specified host and port combination. Also, ensure that an RMI registry has been started at the specified port. ERROR: IOException trying to connect to JMX Connector Server: Failed to retrieve RMIServer stub: javax.naming.NameNotFoundException: jmx-rmi This  NameNotFoundException was a result of inadvertently mistyping the client's String URL portion ( jmx-rmi specified by the client rather than  jmxrmi without the hyphen). This is different from the above error because a JMX Connector Server is at the specified host and port, but with a different String portion. Because multiple JMX Connector Servers can use the same host/port combination as long as they have unique String portions of their URLs, it is not surprising that no assumptions can be made about what String was intended. Exception in thread "main" java.lang.ClassCastException: com.sun.jndi.rmi.registry.RegistryContext cannot be cast to javax.management.remote.rmi.RMIServer The previous exception demonstrated the result of attempting to access a JMX Connector Server from a client with the correct host/port comibination, but with an incorrect String URL portion. This ClassCastException is the result of not specifying any String URL portion at all in the  JMXServiceURL. In other words, the  jmxrmi portion was left off the end of the client's  JMXServiceURL. java.io.IOException: Cannot bind to URL [rmi://localhost:1099/jmxrmi]: javax.naming.NameAlreadyBoundExceptionjmxrmi [Root exception is java.rmi.AlreadyBoundException: jmxrmi] Until now, all of the previously covered exceptions resulted from a JMX Client using a mistyped or otherwise incorrect  JMXServiceURL that did not match that used by the JMX Connector Server. This exception, however, is a JMX Connector Server exception that results from two different applications trying to use the same JMXServiceURL on the same host/port with the same URL String. ERROR: Problem with JMXServiceURL based on rmi:///jndi/rmi://localhost:1099/jmxrmi:Service URL must start with service:jmx: This error message is particularly descriptive and occurs when the  JMXServiceURL does not begin with the specification-required  service:jmx: portion. See Section 13.8 ("Connector Server Addresses") for additional details on all  JMXServiceURLs beginning with this portion. ERROR: Problem with JMXServiceURL based on service:jmx::///jndi/rmi://localhost:1099/jmxrmi: Missing or invalid protocol name: "" As the error message indicates, the protocol is missing from the  JMXServiceURL, which should be service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi, but the portion in  red was omitted. ERROR: Problem with JMXServiceURL based on service:jmx:nothing:///jndi/rmi://localhost:1099/jmxrmi: Unsupported protocol: nothing Whereas the previous error message indicated a missing protocol designation, this error indicates an erroneous protocol specification. In this particular case, the incorrect  JMXServiceURL used was service:jmx:nothing:///jndi/rmi://localhost:1099/jmxrmi where the "nothing" in  red should have been "rmi". ERROR: IOException trying to connect to JMX Connector Server: Failed to retrieve RMIServer stub: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial This error message is displayed for the case where the second protocol listing in the  JMXServiceURL is either omitted or has an incorrect value. In other words, if the  highlighted portion of the correct JMXServiceURL ( service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi)was omitted entirely or had something unexpected like "nothing," this error message would occur. Other Remote JMX Exceptions/Errors Considerations There are, of course, several other types of exceptions one can run into when remotely accessing a JMX Connector Server. These exceptions, which have not been covered here, are those that are thrown when the connection is successfully made, but the sought-after MBean cannot be found or some other problem occurs. The  JMX Best Practices document outlines recommendations related to MBeans throwing standard exceptions and also recommends having MBeans intended for remote management/monitoring implement interfaces with operations that use the  throws  IOException clause (more granular for client than available as  UndeclaredThrowableException). JMX Exceptions in General The focus of this blog entry has been on common exceptions and errors encountered when running a custom JMX Connector Client with a custom JMX Connector Server. Note that JMX spells out its own exception hierarchy related to the JMX Agent. Section 6.4 (JMX Exceptions) of the JMX 1.4 Specification is devoted to these JMX exceptions. The section talks about the  JMException, which is the main class and parent of all descendant exception classes (exception runtime exceptions) thrown by a JMX agent implementation. The JMX runtime exceptions are represented by  JMRuntimeException and its descendant exceptions.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值