use of org.apache.activemq.artemis.core.persistence.OperationContext in project activemq-artemis by apache.
the class StompPluginTest method createServer.
@Override
protected JMSServerManager createServer() throws Exception {
JMSServerManager server = super.createServer();
server.getActiveMQServer().registerBrokerPlugin(verifier);
server.getActiveMQServer().registerBrokerPlugin(new ActiveMQServerPlugin() {
@Override
public void beforeCreateSession(String name, String username, int minLargeMessageSize, RemotingConnection connection, boolean autoCommitSends, boolean autoCommitAcks, boolean preAcknowledge, boolean xa, String defaultAddress, SessionCallback callback, boolean autoCreateQueues, OperationContext context, Map<SimpleString, RoutingType> prefixes) throws ActiveMQException {
if (connection instanceof StompConnection) {
stompBeforeCreateSession.set(true);
}
}
@Override
public void beforeCloseSession(ServerSession session, boolean failed) throws ActiveMQException {
if (session.getRemotingConnection() instanceof StompConnection) {
stompBeforeRemoveSession.set(true);
}
}
});
return server;
}
use of org.apache.activemq.artemis.core.persistence.OperationContext in project activemq-artemis by apache.
the class ActiveMQPacketHandler method handleCreateSession.
private void handleCreateSession(final CreateSessionMessage request) {
boolean incompatibleVersion = false;
Packet response;
try {
Version version = server.getVersion();
if (!version.isCompatible(request.getVersion())) {
throw ActiveMQMessageBundle.BUNDLE.incompatibleClientServer();
}
if (!server.isStarted()) {
throw ActiveMQMessageBundle.BUNDLE.serverNotStarted();
}
if (connection.getChannelVersion() == 0) {
connection.setChannelVersion(request.getVersion());
} else if (connection.getChannelVersion() != request.getVersion()) {
ActiveMQServerLogger.LOGGER.incompatibleVersionAfterConnect(request.getVersion(), connection.getChannelVersion());
}
Channel channel = connection.getChannel(request.getSessionChannelID(), request.getWindowSize());
ActiveMQPrincipal activeMQPrincipal = null;
if (request.getUsername() == null) {
activeMQPrincipal = connection.getDefaultActiveMQPrincipal();
}
OperationContext sessionOperationContext = server.newOperationContext();
Map<SimpleString, RoutingType> routingTypeMap = protocolManager.getPrefixes();
CoreSessionCallback sessionCallback = new CoreSessionCallback(request.getName(), protocolManager, channel, connection);
ServerSession session = server.createSession(request.getName(), activeMQPrincipal == null ? request.getUsername() : activeMQPrincipal.getUserName(), activeMQPrincipal == null ? request.getPassword() : activeMQPrincipal.getPassword(), request.getMinLargeMessageSize(), connection, request.isAutoCommitSends(), request.isAutoCommitAcks(), request.isPreAcknowledge(), request.isXA(), request.getDefaultAddress(), sessionCallback, true, sessionOperationContext, routingTypeMap);
ServerProducer serverProducer = new ServerProducerImpl(session.getName(), "CORE", request.getDefaultAddress());
session.addProducer(serverProducer);
ServerSessionPacketHandler handler = new ServerSessionPacketHandler(server, protocolManager, session, server.getStorageManager(), channel);
channel.setHandler(handler);
sessionCallback.setSessionHandler(handler);
// TODO - where is this removed?
protocolManager.addSessionHandler(request.getName(), handler);
response = new CreateSessionResponseMessage(server.getVersion().getIncrementingVersion());
} catch (ActiveMQClusterSecurityException | ActiveMQSecurityException e) {
ActiveMQServerLogger.LOGGER.securityProblemWhileCreatingSession(e.getMessage());
response = new ActiveMQExceptionMessage(e);
} catch (ActiveMQException e) {
if (e.getType() == ActiveMQExceptionType.INCOMPATIBLE_CLIENT_SERVER_VERSIONS) {
incompatibleVersion = true;
logger.debug("Sending ActiveMQException after Incompatible client", e);
} else {
ActiveMQServerLogger.LOGGER.failedToCreateSession(e);
}
response = new ActiveMQExceptionMessage(e);
} catch (Exception e) {
ActiveMQServerLogger.LOGGER.failedToCreateSession(e);
response = new ActiveMQExceptionMessage(new ActiveMQInternalErrorException());
}
// are not compatible
if (incompatibleVersion) {
channel1.sendAndFlush(response);
} else {
channel1.send(response);
}
}
use of org.apache.activemq.artemis.core.persistence.OperationContext in project activemq-artemis by apache.
the class ReplicationTest method testExceptionSettingActionBefore.
@Test
public void testExceptionSettingActionBefore() throws Exception {
OperationContext ctx = OperationContextImpl.getContext(factory);
ctx.storeLineUp();
String msg = "I'm an exception";
ctx.onError(ActiveMQExceptionType.UNBLOCKED.getCode(), msg);
final AtomicInteger lastError = new AtomicInteger(0);
final List<String> msgsResult = new ArrayList<>();
final CountDownLatch latch = new CountDownLatch(1);
ctx.executeOnCompletion(new IOCallback() {
@Override
public void onError(final int errorCode, final String errorMessage) {
lastError.set(errorCode);
msgsResult.add(errorMessage);
latch.countDown();
}
@Override
public void done() {
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Assert.assertEquals(5, lastError.get());
Assert.assertEquals(1, msgsResult.size());
Assert.assertEquals(msg, msgsResult.get(0));
final CountDownLatch latch2 = new CountDownLatch(1);
// Adding the Task after the exception should still throw an exception
ctx.executeOnCompletion(new IOCallback() {
@Override
public void onError(final int errorCode, final String errorMessage) {
lastError.set(errorCode);
msgsResult.add(errorMessage);
latch2.countDown();
}
@Override
public void done() {
}
});
Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));
Assert.assertEquals(2, msgsResult.size());
Assert.assertEquals(msg, msgsResult.get(0));
Assert.assertEquals(msg, msgsResult.get(1));
final CountDownLatch latch3 = new CountDownLatch(1);
ctx.executeOnCompletion(new IOCallback() {
@Override
public void onError(final int errorCode, final String errorMessage) {
}
@Override
public void done() {
latch3.countDown();
}
});
Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));
}
use of org.apache.activemq.artemis.core.persistence.OperationContext in project activemq-artemis by apache.
the class ReplicationTest method testOrderOnNonPersistency.
@Test
public void testOrderOnNonPersistency() throws Exception {
setupServer(true);
final ArrayList<Integer> executions = new ArrayList<>();
StorageManager storage = getStorage();
manager = liveServer.getReplicationManager();
Journal replicatedJournal = new ReplicatedJournal((byte) 1, new FakeJournal(), manager);
int numberOfAdds = 200;
final CountDownLatch latch = new CountDownLatch(numberOfAdds);
OperationContext ctx = storage.getContext();
for (int i = 0; i < numberOfAdds; i++) {
final int nAdd = i;
if (i % 2 == 0) {
replicatedJournal.appendPrepareRecord(i, new FakeData(), false);
}
ctx.executeOnCompletion(new IOCallback() {
@Override
public void onError(final int errorCode, final String errorMessage) {
}
@Override
public void done() {
executions.add(nAdd);
latch.countDown();
}
});
}
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
for (int i = 0; i < numberOfAdds; i++) {
Assert.assertEquals(i, executions.get(i).intValue());
}
Assert.assertEquals(0, manager.getActiveTokens().size());
}
use of org.apache.activemq.artemis.core.persistence.OperationContext in project activemq-artemis by apache.
the class LocalGroupingHandler method propose.
@Override
public Response propose(final Proposal proposal) throws Exception {
OperationContext originalCtx = storageManager.getContext();
try {
// the waitCompletion cannot be done inside an ordered executor or we would starve when the thread pool is full
storageManager.setContext(storageManager.newSingleThreadContext());
if (proposal.getClusterName() == null) {
GroupBinding original = map.get(proposal.getGroupId());
if (original != null) {
original.use();
return new Response(proposal.getGroupId(), original.getClusterName());
} else {
return null;
}
}
boolean addRecord = false;
GroupBinding groupBinding = null;
lock.lock();
try {
groupBinding = map.get(proposal.getGroupId());
if (groupBinding != null) {
groupBinding.use();
// Returning with an alternate cluster name, as it's been already grouped
return new Response(groupBinding.getGroupId(), proposal.getClusterName(), groupBinding.getClusterName());
} else {
addRecord = true;
groupBinding = new GroupBinding(proposal.getGroupId(), proposal.getClusterName());
groupBinding.setId(storageManager.generateID());
List<GroupBinding> newList = new ArrayList<>();
List<GroupBinding> oldList = groupMap.putIfAbsent(groupBinding.getClusterName(), newList);
if (oldList != null) {
newList = oldList;
}
newList.add(groupBinding);
map.put(groupBinding.getGroupId(), groupBinding);
}
} finally {
lock.unlock();
}
// Storing the record outside of any locks
if (addRecord) {
storageManager.addGrouping(groupBinding);
}
return new Response(groupBinding.getGroupId(), groupBinding.getClusterName());
} finally {
storageManager.setContext(originalCtx);
}
}
Aggregations