Skip to content

Commit

Permalink
[ISSUE apache#367] Support prototype spi instance
Browse files Browse the repository at this point in the history
  change producer and consumer spi to prototype
  • Loading branch information
ruanwenjun committed Jul 12, 2021
1 parent d8ac875 commit bcc0079
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.apache.eventmesh.api.AbstractContext;
import org.apache.eventmesh.spi.EventMeshSPI;

@EventMeshSPI
@EventMeshSPI(isSingleton = false)
public interface MeshMQPushConsumer extends Consumer {

void init(Properties keyValue) throws Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.eventmesh.api.RRCallback;
import org.apache.eventmesh.spi.EventMeshSPI;

@EventMeshSPI
@EventMeshSPI(isSingleton = false)
public interface MeshMQProducer extends Producer {

void init(Properties properties) throws Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ public static <T> T getExtension(Class<T> extensionType, String extensionName) {
loadExtensionClass(extensionType);
}
if (!hasInitializeExtension(extensionName)) {
initializeExtension(extensionType, extensionName);
T instance = initializeExtension(extensionType, extensionName);
EventMeshSPI spiAnnotation = extensionType.getAnnotation(EventMeshSPI.class);
if (!spiAnnotation.isSingleton()) {
return instance;
}
EXTENSION_INSTANCE_CACHE.put(extensionName, instance);
}
return (T) EXTENSION_INSTANCE_CACHE.get(extensionName);
}

private static <T> void initializeExtension(Class<T> extensionType, String extensionName) {
@SuppressWarnings("unchecked")
private static <T> T initializeExtension(Class<T> extensionType, String extensionName) {
ConcurrentHashMap<String, Class<?>> extensionClassMap = EXTENSION_CLASS_LOAD_CACHE.get(extensionType);
if (extensionClassMap == null) {
throw new ExtensionException(String.format("Extension type:%s has not been loaded", extensionType));
Expand All @@ -61,7 +67,7 @@ private static <T> void initializeExtension(Class<T> extensionType, String exten
try {
Object extensionObj = aClass.newInstance();
logger.info("initialize extension instance success, extensionType: {}, extensionName: {}", extensionType, extensionName);
EXTENSION_INSTANCE_CACHE.put(extensionName, extensionObj);
return (T) extensionObj;
} catch (InstantiationException | IllegalAccessException e) {
throw new ExtensionException("Extension initialize error", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@
@Target({ElementType.TYPE})
public @interface EventMeshSPI {

/**
* If true, the spi instance is singleton
*/
boolean isSingleton() default false;

}

Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,24 @@

package org.apache.eventmesh.spi;

import org.apache.eventmesh.spi.example.TestPrototypeExtension;
import org.apache.eventmesh.spi.example.TestSingletonExtension;
import org.junit.Assert;
import org.junit.Test;

public class EventMeshExtensionFactoryTest {

@Test
public void getExtension() {
TestExtension extensionA = EventMeshExtensionFactory.getExtension(TestExtension.class, "extensionA");
extensionA.hello();
public void getSingletonExtensionTest() {
TestSingletonExtension extensionA = EventMeshExtensionFactory.getExtension(TestSingletonExtension.class, "singletonExtension");
TestSingletonExtension extensionB = EventMeshExtensionFactory.getExtension(TestSingletonExtension.class, "singletonExtension");
Assert.assertSame(extensionA, extensionB);
}

@Test
public void getPrototypeExtensionTest() {
TestPrototypeExtension prototypeExtensionA = EventMeshExtensionFactory.getExtension(TestPrototypeExtension.class, "prototypeExtension");
TestPrototypeExtension prototypeExtensionB = EventMeshExtensionFactory.getExtension(TestPrototypeExtension.class, "prototypeExtension");
Assert.assertNotSame(prototypeExtensionA, prototypeExtensionB);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.
*/

package org.apache.eventmesh.spi.example;

public class PrototypeExtension implements TestPrototypeExtension {
@Override
public void hello() {
System.out.println("I am PrototypeExtension");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
* limitations under the License.
*/

package org.apache.eventmesh.spi;
package org.apache.eventmesh.spi.example;

public class ExtensionA implements TestExtension {
public class SingletonExtension implements TestSingletonExtension {

@Override
public void hello() {
System.out.println("I am ExtensionA");
System.out.println("I am SingletonExtension");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.
*/

package org.apache.eventmesh.spi.example;

import org.apache.eventmesh.spi.EventMeshSPI;

@EventMeshSPI(isSingleton = false)
public interface TestPrototypeExtension {

void hello();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
* limitations under the License.
*/

package org.apache.eventmesh.spi;
package org.apache.eventmesh.spi.example;

@EventMeshSPI
public interface TestExtension {
import org.apache.eventmesh.spi.EventMeshSPI;

@EventMeshSPI(isSingleton = true)
public interface TestSingletonExtension {

void hello();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

extensionA=org.apache.eventmesh.spi.ExtensionA
prototypeExtension=org.apache.eventmesh.spi.example.PrototypeExtension
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# 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.

singletonExtension=org.apache.eventmesh.spi.example.SingletonExtension

0 comments on commit bcc0079

Please sign in to comment.