ActiveMQ series (1) and activemq Series
It's really hard to get off the Internet.
For common businesses, data concurrency is a headache. In many cases, resource sharing may cause thread blocking. At this time, the problem arises, and the boss is also following, I am in trouble. Where is the money ?, To be specific, for example, when multiple users place orders and pay for the order at the same time, this is all the silver in the white flowers. It is wrong. The above situation has actually occurred, another problem is dirty Data Reading. How can this problem be solved? From small to large, teachers always let us queue and sit in the classroom in an orderly manner, which also improves efficiency. ActiveMQ is done, in the form of queues, similar to thread pool queues, redis push prop, of course, redis is more fierce.
Windows 10 Enterprise Edition 64-bit
A) http://activemq.apache.org/select your favorite version (or at the bottom of the article, I uploaded, is the latest version of the current), pay attention to the domain name, apache, so it is certainly inseparable from jdk, jdk get:
Http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html, while configuring JAVA_HOME environment B) install ActiveMQ: first, extract the downloaded zip file package, the file directory is as follows: bin needless to say, and tomcat the same, the batch processing is started or stopped by bat and related jar packages. As the name suggests, conf stores the data of the configuration file. What data? Logs and other files generated during execution
Docs is now used for installation and use:
Note in the doc Folder: After the command is executed, you can skip the following configuration. Because it is partially incorrect, type Http: // 127.0.0.1 :( port number) 8161 in the address bar of the browser, at this time, you can see the following interface: configure the default user name and password. The previous step prompts account/password-> admin/admin. The following interface is displayed: ----- In fact, during the test, if there are no special requirements for the above items, you can directly use them without changing them. Note the following: Execute activemq in the bin folder. after bat, I found that it was not executed in this place. At first I thought it was an exception, but it wasn't actually a problem. I just needed to write the code directly.
- 5. Refer to the p2p case code:
Import: Apache. NMS. dll and Apache. NMS. ActiveMQ. dll. (The case is from the bottom link. Although it is something of others, it is impossible to write it and read only what others are .) : Console program: Server
Namespace TestActiveMQ {class Program {private static IConnectionFactory factory // Apache must be referenced. NMS; = new ConnectionFactory ("tcp: // localhost: 61616"); // Apache must be referenced. NMS. activeMQ; static void Main (string [] args) {// establish a connection using (IConnection connection = factory. createConnection () {// create session using (ISession session = connection. createSession () {// create a producer through a session. The new method is the Queue IMessageProdu in MQ. Cer producer = session. createProducer (new Apache. NMS. activeMQ. commands. activeMQQueue ("firstQueue"); // create a message object to be sent ITextMessage message = producer. createTextMessage (); // assign the actual message to this object. text = "12321321321"; // It is very important to set the attributes of the message object. It is the filter condition of Queue and the unique message attribute of P2P messages. properties. setString ("filter", "demo"); // The producer sends messages. The following enumerated parameters are used to determine whether the MsgDeliveryMode is a long chain, and the MsgPriority parameter is the priority level. The minimum sending unit is, of course there are other heavy-load producer. send (message, MsgDeliveryMode. NonPersistent, MsgPriority. Normal, TimeSpan. MinValue); Console. WriteLine ("sent successfully! "); Console. ReadLine ();}}}}}
Client:
Public partial class Form1: Form {public Form1 () {InitializeComponent (); InitConsumer ();} public void InitConsumer () {// create the connection factory IConnectionFactory factory = new ConnectionFactory ("tcp: // localhost: 61616"); // build the connection IConnection connection = factory through the factory. createConnection (); // This is the connection client name. clientId = "firstQueueListener"; // start the connection. If listening, you must start the connection. start (); // create a session ISession session = connection through a connection. createSession (); // creates a consumer through a session. Here, the IMessageConsumer consumer = session is set as the listener parameter of the session type such as Queue. createConsumer (new Apache. NMS. activeMQ. commands. activeMQQueue ("firstQueue"), "filter = 'Demo'"); // register the listener event consumer. listener + = new MessageListener (consumer_Listener); // connection. stop (); // connection. close ();} void consumer_Listener (IMessage message) {ITextMessage msg = (ITextMessage) message; // asynchronous call; otherwise, it cannot return to the main thread tbReceiveMessage. invoke (new DelegateRevMessage (RevMessage), msg);} public delegate void DelegateRevMessage (ITextMessage message); public void RevMessage (ITextMessage message) {tbReceiveMessage. text + = string. format (@ "Received: {0} {1}", message. text, Environment. newLine );}}
Execution result: