use of org.apache.activemq.artemis.core.server.impl.ServerSessionImpl in project activemq-artemis by apache.
the class MQTTConnectionManager method createServerSession.
/**
* Creates an internal Server Session.
*
* @param username
* @param password
* @return
* @throws Exception
*/
ServerSessionImpl createServerSession(String username, String password) throws Exception {
String id = UUIDGenerator.getInstance().generateStringUUID();
ActiveMQServer server = session.getServer();
ServerSession serverSession = server.createSession(id, username, password, ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, session.getConnection(), MQTTUtil.SESSION_AUTO_COMMIT_SENDS, MQTTUtil.SESSION_AUTO_COMMIT_ACKS, MQTTUtil.SESSION_PREACKNOWLEDGE, MQTTUtil.SESSION_XA, null, session.getSessionCallback(), MQTTUtil.SESSION_AUTO_CREATE_QUEUE, server.newOperationContext(), session.getProtocolManager().getPrefixes());
return (ServerSessionImpl) serverSession;
}
use of org.apache.activemq.artemis.core.server.impl.ServerSessionImpl in project activemq-artemis by apache.
the class TemporaryQueueTest method testBlockingWithTemporaryQueue.
@Test
public void testBlockingWithTemporaryQueue() throws Exception {
AddressSettings setting = new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.BLOCK).setMaxSizeBytes(1024 * 1024);
server.getAddressSettingsRepository().addMatch("TestAD", setting);
ClientSessionFactory consumerCF = createSessionFactory(locator);
ClientSession consumerSession = consumerCF.createSession(true, true);
consumerSession.addMetaData("consumer", "consumer");
consumerSession.createTemporaryQueue("TestAD", "Q1");
consumerSession.createConsumer("Q1");
consumerSession.start();
final ClientProducerImpl prod = (ClientProducerImpl) session.createProducer("TestAD");
final AtomicInteger errors = new AtomicInteger(0);
final AtomicInteger msgs = new AtomicInteger(0);
final int TOTAL_MSG = 1000;
Thread t = new Thread() {
@Override
public void run() {
try {
for (int i = 0; i < TOTAL_MSG; i++) {
ClientMessage msg = session.createMessage(false);
msg.getBodyBuffer().writeBytes(new byte[1024]);
prod.send(msg);
msgs.incrementAndGet();
}
} catch (Throwable e) {
e.printStackTrace();
errors.incrementAndGet();
}
System.out.println("done");
}
};
t.start();
while (msgs.get() == 0) {
Thread.sleep(100);
}
int blockedTime = 0;
// https://issues.apache.org/jira/browse/ARTEMIS-368
while (t.isAlive() && errors.get() == 0 && (!prod.getProducerCredits().isBlocked() || blockedTime < 60)) {
if (prod.getProducerCredits().isBlocked()) {
blockedTime++;
} else {
blockedTime = 0;
}
Thread.sleep(100);
}
assertEquals(0, errors.get());
ClientSessionFactory newConsumerCF = createSessionFactory(locator);
ClientSession newConsumerSession = newConsumerCF.createSession(true, true);
newConsumerSession.createTemporaryQueue("TestAD", "Q2");
ClientConsumer newConsumer = newConsumerSession.createConsumer("Q2");
newConsumerSession.start();
int toReceive = TOTAL_MSG - msgs.get();
for (ServerSession sessionIterator : server.getSessions()) {
if (sessionIterator.getMetaData("consumer") != null) {
System.out.println("Failing session");
ServerSessionImpl impl = (ServerSessionImpl) sessionIterator;
impl.getRemotingConnection().fail(new ActiveMQDisconnectedException("failure e"));
}
}
int secondReceive = 0;
ClientMessage msg = null;
while (secondReceive < toReceive && (msg = newConsumer.receive(5000)) != null) {
msg.acknowledge();
secondReceive++;
}
assertNull(newConsumer.receiveImmediate());
assertEquals(toReceive, secondReceive);
t.join();
}
use of org.apache.activemq.artemis.core.server.impl.ServerSessionImpl in project activemq-artemis by apache.
the class MQTTConnectionManager method connect.
/**
* Handles the connect packet. See spec for details on each of parameters.
*/
synchronized void connect(String cId, String username, byte[] passwordInBytes, boolean will, byte[] willMessage, String willTopic, boolean willRetain, int willQosLevel, boolean cleanSession) throws Exception {
String clientId = validateClientId(cId, cleanSession);
if (clientId == null) {
session.getProtocolHandler().sendConnack(MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED);
session.getProtocolHandler().disconnect(true);
return;
}
session.setSessionState(getSessionState(clientId));
String password = passwordInBytes == null ? null : new String(passwordInBytes, CharsetUtil.UTF_8);
session.getConnection().setClientID(clientId);
ServerSessionImpl serverSession = createServerSession(username, password);
serverSession.start();
session.setServerSession(serverSession);
session.setIsClean(cleanSession);
if (will) {
isWill = true;
this.willMessage = ByteBufAllocator.DEFAULT.buffer(willMessage.length);
this.willMessage.writeBytes(willMessage);
this.willQoSLevel = willQosLevel;
this.willRetain = willRetain;
this.willTopic = willTopic;
}
session.getConnection().setConnected(true);
session.start();
session.getProtocolHandler().sendConnack(MqttConnectReturnCode.CONNECTION_ACCEPTED);
}
use of org.apache.activemq.artemis.core.server.impl.ServerSessionImpl in project activemq-artemis by apache.
the class MQTTPublishManager method stop.
synchronized void stop() throws Exception {
ServerSessionImpl serversession = session.getServerSession();
if (serversession != null) {
serversession.removeProducer(serversession.getName());
}
if (managementConsumer != null) {
managementConsumer.removeItself();
managementConsumer.setStarted(false);
managementConsumer.close(false);
}
}
use of org.apache.activemq.artemis.core.server.impl.ServerSessionImpl in project activemq-artemis by apache.
the class StompSession method sendInternalLarge.
public void sendInternalLarge(CoreMessage message, boolean direct) throws Exception {
int headerSize = message.getHeadersAndPropertiesEncodeSize();
if (headerSize >= connection.getMinLargeMessageSize()) {
throw BUNDLE.headerTooBig();
}
StorageManager storageManager = ((ServerSessionImpl) session).getStorageManager();
long id = storageManager.generateID();
LargeServerMessage largeMessage = storageManager.createLargeMessage(id, message);
ActiveMQBuffer body = message.getReadOnlyBodyBuffer();
byte[] bytes = new byte[body.readableBytes()];
body.readBytes(bytes);
largeMessage.addBytes(bytes);
largeMessage.releaseResources();
largeMessage.putLongProperty(Message.HDR_LARGE_BODY_SIZE, bytes.length);
session.send(largeMessage, direct);
largeMessage = null;
}
Aggregations