2. Тексты программ
2.1. AppLab4.java
package com.github.gay.lab_4;
import java.util.Scanner;
public class AppLab4
{
public static void main(String[] args) throws Exception
{
if(args.length != 4)
{
System.out.println("Usage: factoryName queueName username typeSenderOrRecieverOrBoth");
System.exit(1);
}
Sender sender = null;
Receiver receiver = null;
if(args[3].equals("sen") || args[3].equals("both"))
{
sender = new Sender(args[0], args[1], args[2]);
}
if(args[3].equals("rec") || args[3].equals("both"))
{
receiver = new Receiver(args[0], args[1]);
}
/*
Sender sender = new Sender(args[0], args[1], args[2]);
Receiver receiver = new Receiver(args[0], args[1], sender);
*/
Scanner scanner = new Scanner(System.in);
while(true)
{
String line = scanner.nextLine();
if(line.equalsIgnoreCase("exit"))
{
scanner.close();
if(args[3].equals("sen") || args[3].equals("both"))
{
sender.close();
}
if(args[3].equals("rec") || args[3].equals("both"))
{
receiver.close();
}
System.exit(0);
}
if(args[3].equals("sen") || args[3].equals("both"))
{
sender.sendMessage(line);
}
}
}
}
2.2. Receiver.java
package com.github.gay.lab_4;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.QueueReceiver;
import javax.naming.InitialContext;
public class Receiver implements MessageListener
{
QueueConnection connection;
Sender localSender;
InitialContext ctx;
QueueConnectionFactory connFactory;
QueueSession session;
Queue chatQueue;
QueueReceiver receiver;
TextMessage textMessage;
public Receiver(String queueFactory, String queueName) throws Exception
{
//localSender = mainSender;
ctx = new InitialContext();
connFactory = (QueueConnectionFactory) ctx.lookup(queueFactory);
connection = connFactory.createQueueConnection();
session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
chatQueue = (Queue) ctx.lookup(queueName);
receiver = session.createReceiver(chatQueue);
receiver.setMessageListener(this);
connection.start();
}
@Override
public void onMessage(Message message)
{
try
{
textMessage = (TextMessage) message;
System.out.println("Received: " + textMessage.getText());
//localSender.sendMessage("Message \"" + textMessage + "\" has been recived");
}
catch (JMSException exception)
{
exception.printStackTrace();
}
}
public void close() throws JMSException
{
connection.close();
}
}
2.3. Sender.java
package com.github.gay.lab_4;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Sender
{
QueueConnection connection;
QueueSender sender;
QueueSession session;
String username;
InitialContext ctx;
QueueConnectionFactory connFactory;
Queue chatQueue;
TextMessage message;
public Sender(String queueFactory, String queueName, String username) throws NamingException, JMSException
{
ctx = new InitialContext();
connFactory = (QueueConnectionFactory) ctx.lookup(queueFactory);
connection = connFactory.createQueueConnection();
session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
chatQueue = (Queue) ctx.lookup(queueName);
sender = session.createSender(chatQueue);
this.username = username;
}
public void close() throws JMSException
{
connection.close();
}
public void sendMessage(String text) throws JMSException
{
message = session.createTextMessage(username + ": " + text);
sender.send(message);
}
}
2.4. build.gradle
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
apply plugin: 'eclipse'
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'maven-publish'
version = '0.1'
jar {
from {
configurations.runtimeClasspath.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Title': 'lab_4', 'Version': version
attributes 'Main-Class': 'com.github.gay.lab_4.AppLab4'
}
}
repositories {
mavenLocal()
mavenCentral()
}
test {
// show standard out and error on console
testLogging.showStandardStreams = true
// listen to events in the test execution lifecycle
beforeTest { descriptor ->
logger.lifecycle("Running test: " + descriptor)
}
// listen to standard out and standard error of the test JVM(s)
onOutput { descriptor, event ->
logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message )
}
}
dependencies {
compileClasspath 'org.apache.activemq:activemq-all:5.17.3',
'org.apache.logging.log4j:log4j-api:2.19.0',
'org.apache.logging.log4j:log4j-core:2.19.0'
//'org.apache.logging.log4j:log4j-core:2.17.1'
implementation files('libs/jms-1.1.jar',
'libs/activemq-all-5.17.3.jar',
//'libs/log4j-api-2.1.jar',
//'libs/log4j-core-2.17.1.jar',
'libs/log4j-api-2.19.0.jar',
'libs/log4j-core-2.19.0.jar')
implementation 'org.apache.logging.log4j:log4j-api:2.19.0'
implementation 'org.apache.logging.log4j:log4j-core:2.19.0'
//'org.apache.logging.log4j:log4j-api:2.1',
//'org.apache.logging.log4j:log4j-core:2.17.1'
testImplementation 'junit:junit:4.+'
}
jar {
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
}
2.5. jndi.properties
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url = tcp://localhost:61616
java.naming.security.principal = system
java.naming.security.credentials = manager
connectionFactoryNames = QueueCF
queue.queue1 = jms.queue1
2.6. settings.gradle
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
rootProject.name = "lab_4"