我正在尝试使用Jackson库序列化Java动态代理,但是我收到此错误:
public interface IPlanet {
String getName();
}
Planet implements IPlanet {
private String name;
public String getName(){return name;}
public String setName(String iName){name = iName;}
}
IPlanet ip = ObjectsUtil.getProxy(IPlanet.class, p);
ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(ip);
//The proxy generation utility is implemented in this way:
/**
* Create new proxy object that give the access only to the method of the specified
* interface.
*
* @param type
* @param obj
* @return
*/
public static <T> T getProxy(Class<T> …Run Code Online (Sandbox Code Playgroud) .NET远程处理可以为具有可序列化成员的接口生成透明代理远程控制器,例如:
public interface INetworkInterface {
bool login(string username, string password);
bool ExecuteSomething(string command);
}
Run Code Online (Sandbox Code Playgroud)
我想为任何IEnumerator<T>返回结果提供自定义序列化器和活动代理 - 远程处理程序.我的自定义代理远程处理器将通过网络一次处理批量特定数量的元素.例如,给定以下接口:
public interface INetworkInterface2 {
IEnumerable<string> ExecuteSomething(string command);
}
Run Code Online (Sandbox Code Playgroud)
我想提供一个自定义的序列化程序和代理,它可以IEnumerator<T> where T:ISerializable在远程界面中出现时自动使用,并序列化IEnumerator<string>到客户端EnumeratorBatchHandlerClient<string>,它有一个.NET远程代理到服务器端EnumeratorBatchHandlerServer<string>.
我认为客户端批处理程序看起来像这样:
class EnumeratorBatchHandlerClient<T> : IEnumerable<T> {
List<T> batched_results = new List<T>();
EnumerableBatchHandlerServer server_proxy;
T cur = null;
bool MoveNext() {
if (batched_results.Count == 0) {
batched_results = server_proxy.getNextBatch().ToList();
if (batched_results.Count == 0) {
return false; // we are really out of results …Run Code Online (Sandbox Code Playgroud) 我想知道newProxyInstance在创建动态代理实例时何时调用该方法,究竟是什么ClassLoader参数?
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException
Run Code Online (Sandbox Code Playgroud)
非常感谢!
PS我不确定如何正确使用代码格式标签.
我今天才开始使用 DynamicProxy2。并发现它导致了显着的性能下降。
请参阅下面的代码。Test1 比 Test2 慢 10 倍。
使用 DynamicProxy 时有什么提高性能的技巧吗?
class Program
{
public void Main()
{
for (int i = 0; i < 3; i++)
{
var stopWatch = Stopwatch.StartNew();
int count = 1 * 1000 * 1000;
Test1(count);
//Test2(count);
long t = stopWatch.ElapsedMilliseconds;
Console.WriteLine(t.ToString() + " milliseconds");
Console.WriteLine(((double)count/(t/1000)).ToString() + " records/1 seconds");
}
}
void Test1(int count)
{
var builder = new ContainerBuilder();
builder.RegisterType<TestViewModel>()
.EnableClassInterceptors()
.InterceptedBy(typeof(NotifyPropertyChangedInterceptor));
builder.RegisterType<NotifyPropertyChangedInterceptor>();
var container = builder.Build();
for (int i = 0; i …Run Code Online (Sandbox Code Playgroud) 我需要创建一个拦截类中属性的代理.我知道如何使用接口创建一个带有Emit的动态代理,但是如果我没有接口呢?我见过使用RealProxy的示例(如下所示:有没有办法在设置类的任何属性时调用方法?)但是是否可以使用类型生成和发出来实现相同的功能?如果可能的话,我不希望具体类的"所有者"看到任何MarshalByRefObject的痕迹(见下文)......
我相信Castle能够做到这一点,但也许它正在使用RealProxy?
User user = Create<User>();
public class User
{
public string Name { get; set; }
}
public T Create<T>()
{
//magic happens here... :)
return (T)GenerateInterceptingProxyFromT(typeof(T));
}
Run Code Online (Sandbox Code Playgroud) 我开始使用Castle DynamicProxy,我有这个示例来跟踪对象属性的更改.
问题:
码:
class Program
{
static void Main(string[] args)
{
var p = new Person { Name = "Jay" }.AsTrackable();
//here's changed properties list should be empty.
var changedProperties = p.GetChangedProperties();
p.Name = "May";
//here's changed properties list should have one item.
changedProperties = p.GetChangedProperties();
}
}
public static class Ext
{
public static T AsTrackable<T>(this T instance) where T : class
{
return new ProxyGenerator().CreateClassProxyWithTarget
(
instance,
new PropertyChangeTrackingInterceptor()
);
}
public …Run Code Online (Sandbox Code Playgroud) 我想为 BCL 中的类型创建动态代理,该类型是具有内部构造函数的抽象类。我一直是城堡的动态代理,这失败了,但有一个例外,指出没有无参数构造函数(它们是 - 它是内部的)。
有没有办法用城堡来实现这一目标?如果不是,其他任何动态代理框架都能够做到这一点吗?这是开发的开始,因此更改框架很容易。
我想了解方法proxy论证的目的.invokejava.lang.reflect.InvocationHandler
this不用呢?前段时间我发布了一个开源库,它严重依赖Castle DynamicProxy。现在 .NET Core 1.x RTM 出来了,我不知道如何支持 .NET Core(Castle DynamicProxy 仍然不能在 .NET Core 上工作......),直到我遇到了DispatchProxyclass。
实际上没有关于整个类的全面文档,与Castle DynamicProxy相比,它似乎太有限了。
就我而言,我需要拦截属性设置器,并在运行时在生成的代理中实现一些接口。
是否DispatchProxy满足这些要求?或者,在 .NET Core 上运行时生成代理的替代方法是什么?
下面的@Retryable 代码适用于直接调用方法的地方,但是通过@Async 注释方法调用可重试方法然后抛出异常。有什么建议 ?
这是我的服务类
@Service
public class RetryAndRecoverService {
int counter = 0;
String str = null;
@Retryable(value = {FooException.class, BarException.class}, maxAttempts = 5, backoff = @Backoff(delay = 1000, multiplier = 1))
public String retryWithException() {
System.out.println("retryWithException - "+(counter++));
String value = getMapperValue();
if(value == null){
throw new FooException();
}
return value;
}
private String getMapperValue() {
return null;
}
@Async
public String testRetry(){
return retryWithException();
}
}
Run Code Online (Sandbox Code Playgroud)
这是 Junit 测试类
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = RetryExampleApplication.class)
public class RetryTest { …Run Code Online (Sandbox Code Playgroud) dynamic-proxy ×10
c# ×4
java ×3
.net ×2
.net-core ×1
aop ×1
async-await ×1
autofac ×1
castle ×1
dynamic ×1
jackson ×1
json ×1
performance ×1
proxy ×1
spring ×1
spring-boot ×1
spring-retry ×1
wcf ×1